Guest User

Guest User

  • Tech Writer
  • 357
  • 120.8k

Delegate

Jun 28 2012 4:09 AM
Hi

Given the following:-

public delegate void SomeDelegate(int x);

class Program
{

        static void Main(string[] args)
        {


            SomeDelegate delinstance = new SomeDelegate(SomeMethod);
              delinstance.Invoke(3);


           Console.ReadLine();
        }




        public static void SomeMethod(int x)
        {
           Console.WriteLine("The number is...{0}", x);
        }


I am able to assign the Method to the Delegate without having to create a new object like so:

SomeDelegate delinstance = SomeMethod;

Therefore my question is what is the difference if any, apart from a short hand notation?

Ive previously seen code that creates a new object and passes in the method matching the delegate signature as per my 1st example. And other code using the direct assignment ie method to the delegate in the 2nd example...

Regards

Steven

Answers (2)