C# language feature:

  • Method group conversions (delegates)

applies to

  • delegate assignment

benefit

  • Simplifies the syntax used to assign a method to a delegate. Method group conversion allows you to assign the name of a method to a delegate, without the use new or explicitly invoking the delegate’s constructor.

how to use

  • Assign methods to delegates minimizing boilerplate. That is, allow compiler to infer delegate assignment from overloaded method group.

see also

  • Anonymous methods, Delegate inference

example:

    class Method_Group_Conversion_Example
    {
        List<string> StringList = new List<string>();

        void method_group_example()
        {
            StringList.Add("some string");
            // Note that ForEach expects a delegate and a 
            // lambda expression is provided. The
            // compiler unambiguously calls `System.Console.WriteLine(string)` 
            StringList.ForEach(str => System.Console.WriteLine(str)); 

            // even "simpler" way to write without lamda expression
            StringList.ForEach(System.Console.WriteLine);  // example of method group conversion
            // compiler determines based on type of List and method group which method
            // of the various overloaded System.Console.WriteLine methods to resolve to
            // as a string is passed in, method group resolves to `System.Console.WriteLine(string)`
            // by passing, for example System.Console.WriteLine(Char)	
         }
    }