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!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: