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
Explain the difference between the debug class and trace class?
How does foreach loop work in c#?
Is string mutable in c#?
Between windows authentication and sql server authentication, which one is trusted and which one is untrusted?
What does the initial catalog parameter define in the connection string?
Can extension methods access private members?
Explain “static” keyword in c#?
What is difference between association, aggregation and inheritance relationships?
What is the implicit name and type of the parameter that gets passed into the class set method?
Write a sample code to write the contents to text file in c#?
Which debugging tools you can use in the .NET ssSDK?
What are anonymous types in c#?
What is keywords in c#?
Which are the loop types available in c#?
What are the namespace level elements?