473,795 Members | 2,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(stri ng s); //delegate

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

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

Method 2:

listOfDelegates .Add( new MyDelegate(myFu nc1) );
listOfDelegates .Add( new MyDelegate(myFu nc2) );
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 1425
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(stri ng s); //delegate

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

That is, "listOfDelegate s.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 "listOfDelegate s.Add( new
MyDelegate(myFu nc1) );".

--

..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
6544
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? cheers, reed
5
2498
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, so it becomes: handle.onreadystatechange = function() { whatever_was_previously_defined(whatever);some_other_function(handle) };
9
2460
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 mean/do please. JP
8
78099
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? Thanks.
34
3638
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 all of you. What do you want in that web framework(Easy use of Database, Easy use of XML, GUI Designer, etc...)? I'm wating your answers. Thank you for all answers...!
9
3194
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 load with different data (locations, departments, etc.).
2
1274
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 Predicate<Tdelegate in the bcl. It is a required parameter of the List<T>.Find() method. Here is its signature: public delegate bool Predicate<T(T obj) Is there anything inherently different about that Predicate<Tdelegate than
10
1733
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
5594
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 signature for the method (i.e., as below, int Square (int)). Here is an example to show that feature. Note class "UnAwareClass" has its methods Square and Cuber called by a class DelegateClass. This is because these methods in UnAwareClass have the...
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10215
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9043
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.