#385 – A Delegate Instance Can Refer to Both Instance and Static Methods

A delegate instance in C# can refer to one or more methods that all have a particular signature.  You can assign a single method to the delegate using the assignment (=) operator.  You can also add or remove methods from the delegate’s invocation list using the addition assignment (+=) and subtraction assignment (-=) operators.

The delegate can refer to both instance and static methods.  Internally, the delegate will store a reference to either a static method or to an instance method as well as the specific object to invoke the instance method on.

        public delegate void StringDelegate(string s);

        static void Main()
            // First method on invocation list--instance method
            Dog kirby = new Dog("Kirby");
            StringDelegate del = kirby.Bark;

            // 2nd method on invocation list--static method
            del += LogABark;

            // Invoke methods on invocation list--both static and instance methods in this case

        static void LogABark(string bark)
            Console.WriteLine("Logging bark [{0}]", bark);


About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: