c# - Why do generic delegates have DelegateName<T>(T arg)? Why not DelegateName(T arg) -


why generic delegates have delegatename<t>? why not delegatename(t arg)

the argument specifies type, why delegate name has followed <t>, naming convention, or developer know accepts integer or purpose of such c# syntax?

public delegate void mygenericdelegate<t>(t arg);  main() {     // register targets.           mygenericdelegate<string> strtarget = new mygenericdelegate<string>(stringtarget);           strtarget("some string data");        mygenericdelegate<int> inttarget =  new mygenericdelegate<int>(inttarget);           inttarget(9);        static void stringtarget(string arg)      {               console.writeline("arg in uppercase is: {0}", arg.toupper());       }        static void inttarget(int arg)        {                console.writeline("++arg is: {0}", ++arg);      }     } 

a delegate class behind scenes, , needs type specifier generic.

you'll note how declaration of class like:

class mylist {     t[] items;  } 

is invalid, because t unknown in context. delegates need type declaration same reason - t can't resolved type.


consider public delegate void mydel<t>(t arg); - may interesting examine il emitted:

.class public auto ansi sealed consoleapplication20.mydel`1<t>     extends [mscorlib]system.multicastdelegate {     // methods     .method public hidebysig specialname rtspecialname          instance void .ctor (             object 'object',             native int 'method'         ) runtime managed      {     } // end of method mydel`1::.ctor      .method public hidebysig newslot virtual          instance void invoke (             !t arg         ) runtime managed      {     } // end of method mydel`1::invoke      .method public hidebysig newslot virtual          instance class [mscorlib]system.iasyncresult begininvoke (             !t arg,             class [mscorlib]system.asynccallback callback,             object 'object'         ) runtime managed      {     } // end of method mydel`1::begininvoke      .method public hidebysig newslot virtual          instance void endinvoke (             class [mscorlib]system.iasyncresult result         ) runtime managed      {     } // end of method mydel`1::endinvoke  } // end of class consoleapplication20.mydel`1 

Comments