Using a two-parameter lambda with a custom delegate type - CSharp Custom Type

CSharp examples for Custom Type:Lambda

Description

Using a two-parameter lambda with a custom delegate type

Demo Code



using System;/*from  ww  w  .j a v a  2 s . co  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 6. Multiply is defined below.
        Console.WriteLine("\tProduct of {0} * {1} 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