Answer Posted / dotnet user
using System;
// Declare delegate -- defines required signature:
delegate void SampleDelegate(string message);
class MainClass
{
// Regular method that matches signature:
static void SampleDelegateMethod(string message)
{
Console.WriteLine(message);
}
static void Main()
{
// Instantiate delegate with named method:
SampleDelegate d1 = SampleDelegateMethod;
// Instantiate delegate with anonymous method:
SampleDelegate d2 = delegate(string message)
{
Console.WriteLine(message);
};
// Invoke delegate d1:
d1("Hello");
// Invoke delegate d2:
d2(" World");
}
}
| Is This Answer Correct ? | 2 Yes | 1 No |
Post New Answer View All Answers
What is a void c#?
How to Show Message box in Metro Style App?
What is a Command Object in C#?
What is disconnected data architecture in c#?
Why to use lock statement in c#?
Explain when should you call the garbage collector in .net?
What is the advantage of dependency injection?
What is the difference between integer and double?
Is it possible to force garbage collector to run?
What is the use of thread join in c#?
What is msil in c#?
Is arraylist type safe in c#?
What is action in c# 3.5?
How do you prevent a class from being inherited in c#?
Why are local variables stored in stack?