What is delegates & multicast delegate?

Answer Posted / neerajtyagi

delegate void Del(string s);

class TestClass
{
static void Hello(string s)
{
System.Console.WriteLine(" Hello, {0}!", s);
}

static void Goodbye(string s)
{
System.Console.WriteLine(" Goodbye, {0}!", s);
}

static void Main()
{
Del a, b, c, d;

// Create the delegate object a that references
// the method Hello:
a = Hello;

// Create the delegate object b that references
// the method Goodbye:
b = Goodbye;

// The two delegates, a and b, are composed to form
c:
c = a + b;

// Remove a from the composed delegate, leaving d,
// which calls only the method Goodbye:
d = c - a;

System.Console.WriteLine("Invoking delegate a:");
a("A");
System.Console.WriteLine("Invoking delegate b:");
b("B");
System.Console.WriteLine("Invoking delegate c:");
c("C");
System.Console.WriteLine("Invoking delegate d:");
d("D");
}
}


// Output will be

Invoking delegate a:
Hello, A!
Invoking delegate b:
Goodbye, B!
Invoking delegate c:
Hello, C!
Goodbye, C!
Invoking delegate d:
Goodbye, D!

Is This Answer Correct ?    15 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is event delegate in c#?

579


Can hashtable have duplicate keys?

495


Write a program in C# for checking a given number is PRIME or not.

630


What is the max value of int32 in c#?

492


What basic steps are needed to display a simple report in crystal?

536






what is IDisposal interface,IComparable,IEquatable,IFormatable

529


Explain the difference between user control and custom control. Also, explain their use.

588


What is overriding in c#?

492


How more than one version of an assembly can keep in same place?

514


What is difference between c sharp and c#?

468


What Is A Multicast Delegate?

547


What’s thread.sleep() in threading ?

505


Define an abstract class?

506


Does unity use c++ or c#?

493


What are get and set in c#?

578