Using raw delegates, anonymous methods, and lambda expressions - CSharp Custom Type

CSharp examples for Custom Type:Lambda

Description

Using raw delegates, anonymous methods, and lambda expressions

Demo Code



using System;//from w w  w . j  av  a2 s  . co m
using System.Collections.Generic;
delegate void DoIt(string msg);
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" };

        DoIt doSomethingOld = new DoIt(PrintThis);
        if (doSomethingOld != null)
        {
            doSomethingOld("\tThe C# 1.x way: Hello, I'm a cool new delegate!");  // Invoke the delegate.
        }
    }

    static void PrintThis(string message)
    {
        Console.WriteLine(message);
    }
}

Result


Related Tutorials