very simple delegate musing

Posted by Ted on Stack Overflow See other posts from Stack Overflow or by Ted
Published on 2010-04-25T19:58:11Z Indexed on 2010/04/25 20:03 UTC
Read the original article Hit count: 209

Filed under:
|

Sometimes the simplest questions make me love C/C++ and C# more and more.

Today sitting on the bus musing aout delegates I remembered reading somwhere you don't need to use the new keyword when instaniating a new delegate.

For example:

    public static void SomeMethod(string message)
    {
      ...
    }

    ...


   public delegate void TestDelgate(string message); //Define a delegate

    ...........

   //create a new instance ..METHOD 1
   TestDelgate t = new TestDelgate(SomeMethod);  

   //OR another way to create a new instance ..METHOD 2
   TestDelgate t = SomeMethod;   //create a new instance ..METHOD 2

So todays questions are

  1. What happens under the hood in method 2. Does the compiler expand method 2 into method 1, hence writing TestDelgate t = SomeMethod; is just a shortcut for TestDelgate t = new TestDelgate(SomeMethod);, or is there another reason for the exsitence of method 2

  2. Do you guys think method 1 or method 2 is better for readability (this is a subjective question, but I'd just like to get a unscientific feel of general opinion of stackoverflow :-))

© Stack Overflow or respective owner

Related posts about c#

Related posts about delegates