473,804 Members | 2,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Special uses of delegate combinations/removals - your experience, please

hi all,

Can anyone explain (possibly with examples) any special uses of
delegate combinations or removals?

In the very simple example below all delegate combinations/removals can
be done with delegate assignments. So what's the use of having them
around?

delegate string D(string str);

static string f1(string str)
{
return str.ToUpper();
}

static string f2(string str)
{
return str.ToLower();
}

static void Main(string[] args)
{

D d1 = new D(f1);
D d2 = new D(f2);

Console.WriteLi ne(d1("MuluGeta ")); //outputs: MULUGETA
Console.WriteLi ne(d2("MuluGeta ")); //outputs: mulugeta
d1 += d2 + d1 + d1 + d2; // same as d1 = d2 ?

// last is d2, so this outputs: mulugeta
Console.WriteLi ne(d1("MuluGeta "));

// removes d2, thus next output is: MULUGETA
d1 -= d2;
Console.WriteLi ne(d1("MuluGeta "));

}

I appreciate your comments/ideas.
Thanks in advance.

Dec 7 '06 #1
3 1297
Hi,

I think your simple program is too simple and is hiding some important
things..
change the f1 and f2 as this and see whether you see some thing different
and also understand the real usage

static string f1(string str)
{
Console.WriteLi ne("In F1");
return str.ToUpper();
}

static string f2(string str)
{
Console.WriteLi ne("In F2");

return str.ToLower();
}

Nirosh.

"muler" <mu************ *@gmail.comwrot e in message
news:11******** **************@ 80g2000cwy.goog legroups.com...
hi all,

Can anyone explain (possibly with examples) any special uses of
delegate combinations or removals?

In the very simple example below all delegate combinations/removals can
be done with delegate assignments. So what's the use of having them
around?

delegate string D(string str);

static string f1(string str)
{
return str.ToUpper();
}

static string f2(string str)
{
return str.ToLower();
}

static void Main(string[] args)
{

D d1 = new D(f1);
D d2 = new D(f2);

Console.WriteLi ne(d1("MuluGeta ")); //outputs: MULUGETA
Console.WriteLi ne(d2("MuluGeta ")); //outputs: mulugeta
d1 += d2 + d1 + d1 + d2; // same as d1 = d2 ?

// last is d2, so this outputs: mulugeta
Console.WriteLi ne(d1("MuluGeta "));

// removes d2, thus next output is: MULUGETA
d1 -= d2;
Console.WriteLi ne(d1("MuluGeta "));

}

I appreciate your comments/ideas.
Thanks in advance.

Dec 7 '06 #2
tnx! that was a simple, but a nice thing to test. Can someone elaborate
with real examples? I can't find any.

Champika Nirosh wrote:
Hi,

I think your simple program is too simple and is hiding some important
things..
change the f1 and f2 as this and see whether you see some thing different
and also understand the real usage

static string f1(string str)
{
Console.WriteLi ne("In F1");
return str.ToUpper();
}

static string f2(string str)
{
Console.WriteLi ne("In F2");

return str.ToLower();
}

Nirosh.

"muler" <mu************ *@gmail.comwrot e in message
news:11******** **************@ 80g2000cwy.goog legroups.com...
hi all,

Can anyone explain (possibly with examples) any special uses of
delegate combinations or removals?

In the very simple example below all delegate combinations/removals can
be done with delegate assignments. So what's the use of having them
around?

delegate string D(string str);

static string f1(string str)
{
return str.ToUpper();
}

static string f2(string str)
{
return str.ToLower();
}

static void Main(string[] args)
{

D d1 = new D(f1);
D d2 = new D(f2);

Console.WriteLi ne(d1("MuluGeta ")); //outputs: MULUGETA
Console.WriteLi ne(d2("MuluGeta ")); //outputs: mulugeta
d1 += d2 + d1 + d1 + d2; // same as d1 = d2 ?

// last is d2, so this outputs: mulugeta
Console.WriteLi ne(d1("MuluGeta "));

// removes d2, thus next output is: MULUGETA
d1 -= d2;
Console.WriteLi ne(d1("MuluGeta "));

}

I appreciate your comments/ideas.
Thanks in advance.
Dec 7 '06 #3
muler <mu************ *@gmail.comwrot e:
tnx! that was a simple, but a nice thing to test. Can someone elaborate
with real examples? I can't find any.
Any time multiple handlers subscribe to the same event, you end up with
combinations. Note that most of the time when there are multiple
delegates, the return value isn't particularly useful, as only the
final delegate in the list has its return value used.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 7 '06 #4

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

Similar topics

2
4053
by: Jim Schueler | last post by:
Here is some sample code that uses inherited class methods: sub parseHTML::docomment { my $comment = shift ; print $comment, "\n" ; } sub parseASP::AUTOLOAD { use vars qw( $AUTOLOAD ) ;
125
14874
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
36
9489
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
19
7553
by: Thomas Cameron | last post by:
Hello all, I have an array of values which I would like to generate all possible combinations for. And example would be the values "a,b,c". This would produce the following: a ab abc ac
3
2620
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
1
2752
by: CJ Taylor | last post by:
Alright, I probably wont be able to get this question answered, but maybe someone has even more experience than me with delegates... Wow... that sounded fecisious. =) So, I'm trying to dynamically create a delegate where the parameters match on the base level (position is the same, and there base types are the same.) i.e.
5
8634
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file". Same file path containing special characters works fine in one machine, but doesn't work in other. I am using following API function to get short file path. Declare Auto Function GetShortPathName Lib "kernel32" (ByVal lpszLongPath As
3
2328
by: Jeff S | last post by:
Please consider this sample code: It registers a delegate with an event. p1.FirstNameChanged += new Person.NameChanged(p1_FirstNameChanged); Now the following code removes the delegate: p1.FirstNameChanged -= new Person.NameChanged(p1_FirstNameChanged); The above line that removes the delegate confuses me. It works - buy why must we use the 'new' keyword? The delegate already exists and is "registered" with the event. I would think...
6
1405
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is, it's essentially a pointer to a piece of code somewhere else in memory. It therefore (to me anyway) makes sense to define delegates with their signatures and be able to use those signatures at different points in
0
9595
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
10600
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
10097
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7642
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
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4313
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
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
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.