English 中文(简体)
Compiler treatment of a Delegate
原标题:
  • 时间:2009-11-17 01:21:40
  •  标签:
  • c#
  • delegates

If i declare a delegate

public delegate void firstDelegate(string str);

firstDelegate handler = Strfunc;

handler("Hello World");

  ..
  static void Strfunc(string str)
  {
        Console.WriteLine(str);
  }

will the compiler translate the following line

firstDelegate handler=Strfunc;

into

firstDelegate=new firstDelegate(Strfunc);
最佳回答

That s right. Here is the disassembly from reflector:

.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 3
    .locals init (
        [0] class ConsoleApplication4.Program/firstDelegate handler)
    L_0000: nop 
    L_0001: ldnull 
    L_0002: ldftn void ConsoleApplication4.Program::Strfunc(string)
    L_0008: newobj instance void ConsoleApplication4.Program/firstDelegate::.ctor(object, native int)
    L_000d: stloc.0 
    L_000e: ldloc.0 
    L_000f: ldstr "Hello World"
    L_0014: callvirt instance void ConsoleApplication4.Program/firstDelegate::Invoke(string)
    L_0019: nop 
    L_001a: ret 
}

which looks like this in C#:

private static void Main(string[] args)
{
    firstDelegate handler = new firstDelegate(Program.Strfunc);
    handler("Hello World");
}
问题回答

So far I can tell, YES.

This is called as "delegate inference".

BTW, if you want to "append" another function to this delegate, use:

handler += AnotherFunctionName;

And here is the words from C# Pros in the book professional-C#-2008, chapter 7:

For less typing, at every place where a delegate instance is needed, you can just pass the name of the address. This is known by the term delegate inference. This C# feature works as long as the compiler can resolve the delegate instance to a specific type. The code that is created by the C# compiler is the same. The compiler detects that a delegate type is required, so it creates an instance of that delegate type and passes the address of the method to the constructor.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签