Invoke the delegate with an anonymous method. - CSharp Custom Type

CSharp examples for Custom Type:delegate

Description

Invoke the delegate with an anonymous method.

Demo Code




using System;/*from w w w.  ja va  2s . c  o 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" };

        Console.WriteLine("\nExtra 10b. Using an anonymous method: ");
        DoIt doSomethingNewer = delegate (string msg) { Console.WriteLine(msg); };
        if (doSomethingNewer != null)
        {
            doSomethingNewer("\tThe C# 2.0 way: Hello, I'm an anonymous method.");  // Invoke the delegate.
        }
    }
    static void PrintThis(string message)
    {
        Console.WriteLine(message);
    }
}

Result


Related Tutorials