#1,018 – Delegate Invocation Syntax

In C#, you typically invoke a delegate by using the variable referring to the delegate instance as if it were a method.

For example, in the code below, the variable del1 is a delegate instance and you invoke it by using del1 as if it was a method.

        static void Method1(string text)
        {
            Console.WriteLine(text);
        }

        static void Main(string[] args)
        {
            StringHandlerDelegate del1 = Method1;

            // Invoke the delegate instance
            del1("Invoked through delegate");

            Console.ReadLine();
        }

Alternatively, you can call the Invoke method on the delegate instance.  This does exactly the same thing as the example above.

            // Invocation, method #2
            del1.Invoke("Also invoked thru delegate");
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #1,018 – Delegate Invocation Syntax

  1. Pingback: Dew Drop – January 24, 2014 (#1709) | Morning Dew

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

%d bloggers like this: