Click here to Skip to main content
15,997,043 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am working with delegates in one of my project. I know that Delegate is a class (Syatem.Delegate)and delegate is a keyword for the same. But I am not able to understand why I can not create delegate type in method but for Delaget it goes fine in a method(method can not have types defined in the body)
Ex:

C#
public static void PerformFacade(int gradeInput, int method)
{
  MethodInfo mInfo = typeof(CompanyArchFacade).GetMethod("MyMethod");

  // works fine
  Delegate callMethod = Delegate.CreateDelegate(typeof(selectedOperation), mInfo);
  // throws error
  delegate callMethod = Delegate.CreateDelegate(typeof(selectedOperation), mInfo);

  selectedOperation selOp = (selectedOperation)callMethod;
  selOp();          
}


Please advise. Thanks !


Regards,
Divya
Posted
Updated 14-May-13 1:06am
v2

1 solution

No, the keyword delegate and System.Delegate are not the same, not at all.

You could be confused with the C# alias keywords. For example "int" is an alias for System.Int32. For delegates, this is not the case.

Read the documentation on these type and look through all the cases of using the keyword. It's not as trivial as you may think. In particular, don't miss the use of this keyword in anonymous delegates. Please start here:
http://msdn.microsoft.com/en-us/library/ms173171%28v=vs.110%29.aspx[^].

Please see my past answers on the topic:
Pass a method with N params as parameter in C#[^],
What is the Extra Advantage of Delegate[^],
Confusion about delegates[^].

In particular, you should pay special attention for one complicated thing: declare a delegate type with the delegate keyword. Create a delegate instance. Check up the runtime type of the delegate instance using GetType method. You will find that this is the class which has nothing to do with the delegate type!

For some explanations, please see my article: Dynamic Method Dispatcher[^].

—SA
 
Share this answer
 
v3
Comments
Maciej Los 14-May-13 16:51pm    
+5!
Sergey Alexandrovich Kryukov 14-May-13 17:35pm    
Thank you, Maciej.
—SA
Divymital 15-May-13 4:24am    
Thanks Sergey this is pretty clear to me, But while going through some more documentation and some debugging I am not able to understand taht .. when I checked the type of my delegate 'selectedOperation' it is showing multicast delegate ! .. as per one of your post you have mentioned that a delegate points to a code point and to a class of object .. what is the difference between delegate and multicase delegate ?

Also .. why is below code working :

delegate void selectedOperation();
selectedOperation sel = myMethod ; sel()

above code went well for instance method .. even when I did nto supply instance class object.

Thanks !
Divya
Sergey Alexandrovich Kryukov 15-May-13 10:34am    
Its easier to understand how it works than to explain this confusing terminology. First thing to understand is: never ask questions about "difference", they are incorrect. Can you tell us the difference between apple and Apple? :-)

I basically explained it in last two paragraph. If you read my article, you will be able to find some explanation. Delegate as the type is a template defining signature for a handle. None of the objects really have this type. The objects are delegate instances, variables or fields of delegate type. In you case, sel is the delegate of the selectedOperation type, but this type is not a type of sel.

Maybe, don't take is seriously in terms of terminology, which is somewhat fuzzy. It this statement the word "delegate" is used in two different meanings, and even the word "type" is used in different meanings.

The thing is: delegates instances are immutable, as explained in my article. Your sel always has relationship with the selectedOperation "type", where the "type" is used in the sense, different from the usual "type of object", as in the rest of programming. As to the "usual" sense of "type", this variable does not have any type at first, as it is null. When you add the first add a handler to its invocation list (which is not yet even created!), the object is actually created and then it gets some runtime type (single-cast delegate type and then multi-cast delegate). Every time you do it, it's a newly created object. Same thing happens when you remove a handler. These runtime types are actually those very classes which are responsible for supporting of the invocation list and invocation itself, which is the main part of functionality. The "delegate type" like selectedOperation is not a type of these objects, not even related. This is a mechanism for compile-time check of the signature of a handler. No wonder: the signature of handlers are used during invocation and should match.

Such a non-trivial mechanism and messy terminology. At the same time, the usage is simple enough.

I hope I explained it all. It it's clear now, please accept the answer formally (green button).

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900