C# Interview Question #57
What is an anonymous method in C#?
Exceptions, Delegates, Events & Lambdas Mid-Level Intermediate
Quick Interview Answer
An anonymous method is an unnamed method defined using the delegate keyword. It was introduced before lambda expressions and can still be used, although lambdas are usually shorter and more idiomatic in modern C#.
Detailed Explanation
An anonymous method is an unnamed method defined using the delegate keyword. It was introduced before lambda expressions and can still be used, although lambdas are usually shorter and more idiomatic in modern C#.
Both anonymous methods and lambdas can be assigned to compatible delegate types.
Code Example
Action<string> display = delegate(string message)
{
Console.WriteLine(message);
};
display("Hello");