No need to specify callback method parameters

  1. When it is possible don’t specify callback method parameters?
  2. What does the C# compiler generates when there are no references to arguments in anonymous method’s code?

When there are no references to arguments in anonymous method’s code it is possible don’t specify callback method parameters.

 

Example 1:

class Program

    {

 

        private delegate int MyDelegate(int param);

        private static event MyDelegate _myEvent;

 

        static void Main(string[] args)

        {

            _myEvent += new MyDelegate(delegate { Trace.WriteLine("wer"); return 0; });

 

        }

 

    }

However the C# compiler still generates code which matches with its delegate’s signature.

Compiler generated code:

[CompilerGenerated]
private static int <Main>b__0(int)
{
    Trace.WriteLine("wer");
    return 0;

}

[CompilerGenerated]

private static MyDelegate CS$<>9__CachedAnonymousMethodDelegate1;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s