473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Should I instantiate the delegate or not?

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
Aug 20 '07 #1
5 1527
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Carl Johansson" <ca************@nogarbagehallde.comwrote in message
news:Og**************@TK2MSFTNGP03.phx.gbl...
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

Aug 20 '07 #2
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.

Aug 20 '07 #3
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" wrote:
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
Aug 20 '07 #4
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" <ya*********@gmail.comskrev i meddelandet
news:8C**********************************@microsof t.com...
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" wrote:
>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

Aug 21 '07 #5
Carl Johansson <ca************@nogarbagehallde.comwrote:
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/

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sharon | last post by:
I have a delegate definition: public delegate void SomeEventHandler(object obj); To make an instance of it it’s easy: public class myClass { public event SomeEventHandler handler; } But,...
2
by: Nima | last post by:
The documentation for the Delegate and the MultiCastDelegate classes tell me that when we have a line like: public delegate void CheckAndPrintDelegate(string str); it causes the compiler to...
4
by: ^MisterJingo^ | last post by:
Hi all, I've been trying to get my head around delegates. The book i'm using had a single example, not much explaination, and didn't show how to set up a delegate and pass variables in and out...
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
6
by: David Veeneman | last post by:
I have several events that pass a value in their event args. One event passes an int, another a string, another a DateTime, and so on. Rather than creating a separate set of event args for each...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
3
by: Sin Jeong-hun | last post by:
I've been using delegates as class-wide variables, like: class TheForm: Form { delegate void ChangeTextDelegate(string msg); ChangeTextDelegate ctd; public TheForm() { ctd=new...
3
by: Paul | last post by:
I have decided on a basic architechture to quickly refactor two processing functions of my winforms application until we get the chance to rewrite it. Basically it will consist of 2 applications:-...
7
by: RichB | last post by:
I am just trying to get to grips with C# and OOP, and one of the benefits would seem to be code reuse. However I am not sure where to draw the line. I have the following section of code: if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.