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 difference between association, aggregation and inheritance relationships?
What does f mean in c#?
What is datareader c#?
Is hashset serializable c#?
Can you inherit multiple abstract classes in c#?
Can you create an instance of a static class?
How is exception handling implemented in c#?
What is throw in c#?
Can an abstract class inherit from another abstract class c#?
Can we inherit partial class in c#?
What is the difference between internal and private in c#?
Perfect Example Of While And Do-While Loop In C#.Net ?
What is class sortedlist underneath?
What is difference between class and interface in c#?
Why we use oops in c#?