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 the difference between an integer and int?
How do you determine whether a string represents a numeric value?
What are synchronous and asynchronous operations?
Can I define a type that is an alias of another type (like typedef in c++)?
What is the difference between integer and double?
What is the difference between hashtable and dictionary in c#?
What are the types in c#?
How many constructors can a class have in c#?
What are the steps to make an assembly to public?
How do I create a dbml file?
Can we inherit static class in c#?
How big is an int in c#?
Can properties be static in c#?
What is difference between event and delegate in c#?
Constructor to an arbitrary base constructor?