What are Anonymous Methods in C#?
π‘ Concept: Anonymous Method
An anonymous method in C# is a method without a name, defined inline using the delegate
keyword. It can be assigned to a delegate and executed like a normal method.
π Quick Intro
Anonymous methods provide a convenient way to define inline code blocks for short delegate implementations without declaring a separate method. Introduced in C# 2.0, they are often used for event handling and callbacks.
π§ Analogy
Imagine a sticky note with instructions. You donβt give the note a title, you just write the steps. Thatβs like an anonymous method β no formal name, but it does the job right when needed.
π§ Technical Explanation
- βοΈ Anonymous methods are defined using the
delegate
keyword without a name. - π« Cannot have access modifiers or overloads.
- π― Typically used to reduce verbosity in event handlers or callbacks.
- β Can access outer method variables (closures).
- π Often replaced by lambda expressions in modern C#.
π― Use Cases
- β Defining inline event handlers for UI actions.
- β One-off delegate usage for short logic blocks.
- β Improving code readability by reducing boilerplate.
- β Simplifying logic in LINQ queries or threading scenarios.
π» Code Example
public delegate void PrintDelegate(string message);
class Program {
static void Main() {
PrintDelegate print = delegate(string msg) {
Console.WriteLine(""Anonymous says: "" + msg);
};
print(""Hello from anonymous method!"");
}
}

β Interview Q&A
Q1: What is an anonymous method?
A: A method defined inline without a name using the delegate keyword.
Q2: Can anonymous methods access outer variables?
A: Yes, they support closures.
Q3: In which version of C# were anonymous methods introduced?
A: C# 2.0.
Q4: Can you use parameters in anonymous methods?
A: Yes, they can accept parameters like named methods.
Q5: Are anonymous methods the same as lambda expressions?
A: No, but they are similar and often replaced by lambdas.
Q6: Can anonymous methods return values?
A: Yes, if the delegate signature returns a value.
Q7: Can you use async in anonymous methods?
A: Yes, with lambda syntax.
Q8: Are anonymous methods useful for event handling?
A: Yes, they are ideal for inline event handling.
Q9: Can anonymous methods be removed from event subscriptions?
A: No, they cannot be easily removed unless assigned to a variable.
Q10: Do anonymous methods have access modifiers?
A: No, access modifiers arenβt allowed.
π MCQs
Q1. Which C# version introduced anonymous methods?
- C# 1.0
- C# 2.0
- C# 3.0
- C# 4.0
Q2. Which keyword is used to define an anonymous method?
- lambda
- var
- delegate
- method
Q3. Can anonymous methods have names?
- Yes
- No
- Optional
- Only in lambdas
Q4. What is a common replacement for anonymous methods in modern C#?
- Named functions
- Partial classes
- Lambda expressions
- Events
Q5. Can anonymous methods be used with delegates?
- Yes
- No
- Only with events
- Only with generics
Q6. What is a closure in C#?
- A sealed class
- A virtual method
- Anonymous method accessing outer variables
- An async callback
Q7. Can anonymous methods return values?
- No
- Yes
- Only void
- Only from lambdas
Q8. Can anonymous methods be async?
- No
- Yes, with lambdas
- Only static methods
- Only void
Q9. Which keyword cannot be used with anonymous methods?
- delegate
- return
- public
- string
Q10. Are anonymous methods useful for event handling?
- No
- Yes
- Only in UI
- Rarely
π‘ Bonus Insight
Anonymous methods make your code more concise and readable when the logic is short-lived and doesn't require reuse. In modern C#, lambda expressions are preferred for this purpose, but understanding anonymous methods helps you grasp delegate fundamentals.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!