473,396 Members | 1,804 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,396 software developers and data experts.

What's the difference between these two ways of adding delegates to a Generic List object?

Dear CSharp Group,

Both of these techniques work as expected but what is the better way of
doing this? Or does it even make a difference?

Method 1:

public delegate void MyDelegate(string s); //delegate

List<MyDelegatelistOfDelegates = new List<MyDelegate>(); //Generic
List of Delegates

listOfDelegates.Add( myFunc1 );
listOfDelegates.Add( myFunc2 );

Method 2:

listOfDelegates.Add( new MyDelegate(myFunc1) );
listOfDelegates.Add( new MyDelegate(myFunc2) );
So when I loop through my list I can call the functions and they both
work the same. Is one method more better then the other? All I can
tell is that the 2nd method I'm first creating a new Delegate and
passing in my function then adding it. The first method doesn't create
the new Delegate object.

So which is recommended and can I get bad side effects with one way or
the other?

Thanks in advance guys.

Jan 21 '07 #1
1 1401
Deckarep wrote:
Both of these techniques work as expected but what is the better way of
doing this? Or does it even make a difference?
public delegate void MyDelegate(string s); //delegate

List<MyDelegatelistOfDelegates = new List<MyDelegate>(); //Generic
listOfDelegates.Add( myFunc1 );
listOfDelegates.Add( myFunc2 );
listOfDelegates.Add( new MyDelegate(myFunc1) );
listOfDelegates.Add( new MyDelegate(myFunc2) );
No difference, except that your Method 1 is a bit smaller and clearer.

That is, "listOfDelegates.Add( myFunc1 );" uses a simplified syntax,
but if you use Reflector to look at your code, you'll see that it
generates the exact same CIL as "listOfDelegates.Add( new
MyDelegate(myFunc1) );".

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
Jan 21 '07 #2

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

Similar topics

92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
5
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another,...
9
by: Jay | last post by:
Everywhere I go (read/browse) I see these parameters.... ByVal sender As Object, ByVal e As System.EventArgs Yet I never see them used within the function/method. Could someone tell me what they...
8
by: Jeff S. | last post by:
I was recently advised: << Use List<struct> and Find() using different Predicate delegates for your different search needs.>> What is "Predicate delegate" as used in the above recommendation? ...
34
by: emrahayanoglu | last post by:
Hello Everyone, Now, I'm working on a new web framework. I tried many test on the other programming languages. Then i decided to use python on my web framework project. Now i want to listen...
9
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will...
2
by: Bob Johnson | last post by:
It appears to me that the only things that differentiates any delegates is (1) the type name, and (2) the signature (return type or void, and parameters). Is this true? Take, for instance, the...
10
by: timor.super | last post by:
Hi all, Imagine I've an array of int : int anArray = new int; I want to extract all the integer that are superior to 500 I can do :
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.