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

M

muler

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.WriteLine(d1("MuluGeta")); //outputs: MULUGETA
Console.WriteLine(d2("MuluGeta")); //outputs: mulugeta
d1 += d2 + d1 + d1 + d2; // same as d1 = d2 ?

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

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

}

I appreciate your comments/ideas.
Thanks in advance.
 
C

Champika Nirosh

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.WriteLine("In F1");
return str.ToUpper();
}

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

return str.ToLower();
}

Nirosh.
 
M

muler

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

Champika said:
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.WriteLine("In F1");
return str.ToUpper();
}

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

return str.ToLower();
}

Nirosh.

muler said:
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.WriteLine(d1("MuluGeta")); //outputs: MULUGETA
Console.WriteLine(d2("MuluGeta")); //outputs: mulugeta
d1 += d2 + d1 + d1 + d2; // same as d1 = d2 ?

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

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

}

I appreciate your comments/ideas.
Thanks in advance.
 
J

Jon Skeet [C# MVP]

muler said:
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.
 

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

Top