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

Learn More About C# πŸ“š

1. What is C#? πŸ‘‰ Explained
2. Main Features of C# πŸ‘‰ Explained
3. Difference Between C# and Java πŸ‘‰ Explained
4. Common Language Runtime (CLR) in C# πŸ‘‰ Explained
5. Common Type System (CTS) in C# πŸ‘‰ Explained
6. Common Language Specification (CLS) in C# πŸ‘‰ Explained
7. Value Types vs Reference Types in C# πŸ‘‰ Explained
8. What is a Namespace in C#? πŸ‘‰ Explained
9. Purpose of the 'using' Keyword in C# πŸ‘‰ Explained
10. Different Data Types in C# πŸ‘‰ Explained
11. Difference Between int and Int32 in C# πŸ‘‰ Explained
12. Difference Between float, double, and decimal in C# πŸ‘‰ Explained
13. What is the Default Value of a Boolean in C#? πŸ‘‰ Explained
14. What is Boxing and Unboxing in C# πŸ‘‰ Explained
15. What are the Different Types of Operators in C# πŸ‘‰ Explained
16. Difference Between Equals and == in C# πŸ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# πŸ‘‰ Explained
18. What is the Ternary Operator in C# πŸ‘‰ Explained
19. How Does the Switch Statement Work in C# πŸ‘‰ Explained
20. What is Object-Oriented Programming in C# πŸ‘‰ Explained
21. What are the Four Pillars of OOP in C# πŸ‘‰ Explained
22. What is Encapsulation in C# πŸ‘‰ Explained
23. What is Inheritance in C# πŸ‘‰ Explained
24. What is Polymorphism in C# πŸ‘‰ Explained
25. What is Abstraction in C# πŸ‘‰ Explained
26. What is an Abstract Class in C# πŸ‘‰ Explained
27. What is an Interface in C# πŸ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? πŸ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# πŸ‘‰ Explained
30. How Do You Create a Class in C#? πŸ‘‰ Explained
31. What is a Constructor in C# πŸ‘‰ Explained
32. What Are the Types of Constructors in C# πŸ‘‰ Explained
33. What is a Static Constructor in C# πŸ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# πŸ‘‰ Explained
35. What is the Use of 'this' Keyword in C# πŸ‘‰ Explained
36. What is a Destructor in C# πŸ‘‰ Explained
37. What is Object Initializer Syntax in C# πŸ‘‰ Explained
38. What is the Difference Between Field and Property in C# πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup