Misusing example 13 to do addition rather than multiplication - CSharp Custom Type

CSharp examples for Custom Type:Lambda

Description

Misusing example 13 to do addition rather than multiplication

Demo Code




using System;//from w ww  .  j a v a 2  s . c o m
using System.Collections.Generic;
delegate int HandleTwo(int n, int m);
class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
        int[] numArray = numbers.ToArray();
        List<string> words = new List<string> { "one", "two", "three", "four", "five" };

        int result2 = Multiply(2, 3, (n, m) => n + m);    // Writes 5.
        Console.WriteLine("\tSUM of {0} + {1} USING MULTIPLY() is {2}", 2, 3, result2.ToString());

    }
    static int Multiply(int x, int y, HandleTwo proc)
    {
        if (proc == null)
        {
            throw new ArgumentNullException("Null delegate passed.");
        }
        return proc(x, y);
    }
}

Result


Related Tutorials