C# language feature:

  • Delegate inference

applies to

  • delegates

benefit

  • Allows direct assignment of a method to a delegate variable, without first wrapping it in a delegate object.

how to use

  • 1) Define delegate.
  • 2) Declare a function with function body matching delegate signature.
  • 3) Assign function to delegate variable.

see also

  • Anonymous methods, Method group conversions

example:

    delegate void Delegate_Definition(int x); // 1) Define delegate.
    class Delegate_Inference_Example
    {
        // 2) Declare a function with function body matching delegate signature.
        void my_function(int k) { System.Console.WriteLine(k); }

        public void inference_example(){
            // 3) Assign function to delegate variable.
            Delegate_Definition newwaydelegate = my_function;
            
            // Note the above statement is a simpler and equivalent to the following. 
            Delegate_Definition oldwaydelegate = new Delegate_Definition(my_function);
        }
    }