Another Anonymous Method Example - CSharp Custom Type

CSharp examples for Custom Type:Lambda

Description

Another Anonymous Method Example

Demo Code



using System;/*from www  .  j ava  2s  .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 result = Multiply(2, 3, delegate (int n, int m) { return n * m; });
        Console.WriteLine("\tProduct of {0} * {1} is {2}", 2, 3, result);
    }  
    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