Should I instantiate the delegate or not?

  • Thread starter Thread starter Carl Johansson
  • Start date Start date
C

Carl Johansson

Suppose I have a delegate variable, what is the difference between the
following two statements?

DelegateType methodPointer = new DelegateType(SomeMethod);

and...

DelegateType methodPointer = SomeMethod;

Both statements compile, and when the program runs I can't see any
difference in behavior? Is the constructor somehow automatically called in
the second case?

Regards Carl Johansson
 
It's VS2005 being smart for you.
If you were to look at the code through something like Reflector, you'd see
the created code is identical.
 
Carl,

In the second case, the compiler effectively creates the code that you
see in the first case. They do the same thing. C# 2.0 provided was when
the shorthand was introduced.
 
have alook on the ildasm code :

The first assignment

DelegateType methodPointer = new DelegateType(SomeMethod);
==========================================
..method public hidebysig instance void A() cil managed
{
// Code size 15 (0xf)
.maxstack 3
.locals init ([0] class CheckDelegate.Class1/DelegateType methodPointer)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldftn instance void CheckDelegate.Class1::SomeMethod()
IL_0008: newobj instance void
CheckDelegate.Class1/DelegateType::.ctor(object,

native int)
IL_000d: stloc.0
IL_000e: ret
} // end of method Class1::A


The Second assignment :
..method public hidebysig instance void B() cil managed
{
// Code size 15 (0xf)
.maxstack 3
.locals init ([0] class CheckDelegate.Class1/DelegateType methodPointer)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldftn instance void CheckDelegate.Class1::SomeMethod()
IL_0008: newobj instance void
CheckDelegate.Class1/DelegateType::.ctor(object,

native int)
IL_000d: stloc.0
IL_000e: ret
} // end of method Class1::B


As you can see the compiler do the same for both statements.

Conclusion :
They both are the same.
 
Thank you for your replies! Very helpful! I suppose with a tool to create
"ildasm code" I could have drawn this conclusion for myself. What is
"ildasm", "Intermediate Language DisASseMbler"? How can I get such a tool?

Regards Carl Johansson

Yaron Karni said:
have alook on the ildasm code :

The first assignment

DelegateType methodPointer = new DelegateType(SomeMethod);
==========================================
.method public hidebysig instance void A() cil managed
{
// Code size 15 (0xf)
.maxstack 3
.locals init ([0] class CheckDelegate.Class1/DelegateType methodPointer)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldftn instance void CheckDelegate.Class1::SomeMethod()
IL_0008: newobj instance void
CheckDelegate.Class1/DelegateType::.ctor(object,

native int)
IL_000d: stloc.0
IL_000e: ret
} // end of method Class1::A


The Second assignment :
.method public hidebysig instance void B() cil managed
{
// Code size 15 (0xf)
.maxstack 3
.locals init ([0] class CheckDelegate.Class1/DelegateType methodPointer)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldftn instance void CheckDelegate.Class1::SomeMethod()
IL_0008: newobj instance void
CheckDelegate.Class1/DelegateType::.ctor(object,

native int)
IL_000d: stloc.0
IL_000e: ret
} // end of method Class1::B


As you can see the compiler do the same for both statements.

Conclusion :
They both are the same.
--
Sincerely
Yaron Karni
http://dotnetbible.blogspot.com/


Carl Johansson said:
Suppose I have a delegate variable, what is the difference between the
following two statements?

DelegateType methodPointer = new DelegateType(SomeMethod);

and...

DelegateType methodPointer = SomeMethod;

Both statements compile, and when the program runs I can't see any
difference in behavior? Is the constructor somehow automatically called
in
the second case?

Regards Carl Johansson
 
Carl Johansson said:
Thank you for your replies! Very helpful! I suppose with a tool to create
"ildasm code" I could have drawn this conclusion for myself. What is
"ildasm", "Intermediate Language DisASseMbler"? How can I get such a tool?

It's part of the SDK. If you have Visual Studio installed, just open a
Visual Studio Command Prompt and run "ildasm".

Alternatively, download Reflector which can show the IL or decompile to
C# (or VB.NET):
http://www.aisto.com/roeder/dotnet/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top