π Advanced C#: Delegates, Events, and LINQ
As you progress from intermediate to advanced C#, three powerful concepts stand out: Delegates, Events, and LINQ. These features enable you to write flexible, expressive, and high-performance codeβespecially important in real-world applications, game development, and scalable systems.
πΉ 1. Delegates
A delegate is a type that represents references to methods with a specific signature. Think of it as a type-safe function pointer.
β Why Use Delegates?
Enable callback methods
Support event-driven programming
Allow passing methods as parameters
πΈ Example: Basic Delegate
public delegate void GreetDelegate(string name);public class Program{ public static void Greet(string name) { Console.WriteLine($"Hello, {name}"); } public static void Main() { GreetDelegate greet = Greet; greet("Avinash"); }}
πΈ Built-in Delegates
C# provides generic delegates:
Action β No return value
Func β Returns a value
Predicate β Returns bool
Action<string> greet = name => Console.WriteLine($"Hello {name}");Func<int, int, int> add = (a, b) => a + b;
πΉ 2. Events
Events are built on delegates and are used to implement the publisher-subscriber pattern.
β Why Use Events?
Decouple components
Enable reactive programming
Common in UI, game systems, and messaging
πΈ Example: Event Usage
public class Button{ public event Action OnClick; public void Click() { OnClick?.Invoke(); }}public class Program{ public static void Main() { Button btn = new Button(); btn.OnClick += () => Console.WriteLine("Button Clicked!"); btn.Click(); }}
πΈ Real-World Use Cases
UI interactions (button clicks)
Game events (player damage, level up)
Notifications and messaging systems
πΉ 3. LINQ (Language Integrated Query)
LINQ allows you to query collections in a readable and concise way.
β Why Use LINQ?
Simplifies data manipulation
Improves readability
Reduces boilerplate loops
πΈ Example: Basic LINQ Query
var numbers = new List<int> { 1, 2, 3, 4, 5 };var evenNumbers = numbers .Where(n => n % 2 == 0) .Select(n => n * 10) .ToList();
πΈ LINQ with Objects
var users = new List<User>{ new User { Name = "A", Age = 20 }, new User { Name = "B", Age = 30 }};var result = users .Where(u => u.Age > 25) .Select(u => u.Name) .ToList();
πΈ Common LINQ Methods
Where() β Filtering
Select() β Projection
OrderBy() β Sorting
GroupBy() β Grouping
FirstOrDefault() β Safe retrieval
π₯ Combining Delegates, Events, and LINQ
These concepts often work together in modern applications:
public class Game{ public event Action<int> OnScoreUpdated; public void AddScore(int score) { OnScoreUpdated?.Invoke(score); }}// Usagevar game = new Game();game.OnScoreUpdated += score => Console.WriteLine($"Score: {score}");
You can even use LINQ to process event-driven data streams efficiently.
π Best Practices
Prefer built-in delegates (Action, Func) over custom ones
Always unsubscribe from events to avoid memory leaks
Avoid overusing LINQ in performance-critical loops
Use LINQ for readability, not complexity
π Conclusion
Delegates, Events, and LINQ are fundamental building blocks of advanced C# development. They empower you to write expressive, decoupled, and efficient code. Whether you're building enterprise applications, APIs, or games in Unity, mastering these concepts will significantly elevate your development skills.
Start applying them in small components, and gradually integrate them into larger systems for maximum impact.