Wrong explanation in MSDN on C# delegates

At MSDN site, they described about what can be set as delegate this way.

When a delegate is constructed to wrap an instance method, the delegate references both the instance and the method. A delegate has no knowledge of the instance type aside from the method it wraps, so a delegate can refer to any type of object as long as there is a method on that object that matches the delegate signature. When a delegate is constructed to wrap a static method, it only references the method. Consider the following declarations:

public class MethodClass
{
public void Method1(string message) { }
public void Method2(string message) { }
}

Along with the static DelegateMethod shown previously, we now have three methods that can be wrapped by a Del instance.

MethodClass obj = new MethodClass();
Del d1 = obj.Method1;
Del d2 = obj.Method2;

Instead of assigned to obj in the last lines of the codes above, d1 and d2 are assigned to obj.Method1 and obj.Method2.
So, it is not that the delegate can refer to any type of objects as long as blah blah. It just refers to those specific methods.

Let’s be careful.

Advertisement

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

Follow

Get every new post delivered to your Inbox.

Join 39 other followers