Evolution: Delegates to Lambda to Expression Trees

Image
🔸 The Evolution Journey: From Delegates to Lambda to Expression Trees - Understanding how C# evolved from basic function pointers to powerful expression manipulation. Historical Context: 🔸C# started with basic delegates in .NET 1.0 (2002). 🔸Anonymous methods were introduced in C# 2.0 (2005). 🔸Lambda expressions revolutionized the language in C# 3.0 (2007). 🔸Expression trees enabled powerful metaprogramming capabilities. 🔸This evolution made functional programming and LINQ possible. 2002 - C# 1.0 Stage 1: Traditional Delegates 🔸Basic function pointers with explicit method declaration. 🔸Required separate named methods for each delegate instance. 🔸Verbose syntax but type-safe and powerful. 🔸Foundation for event handling and callbacks. // C# 1.0 - Traditional Delegate Approach public delegate bool NumberPredicate(int number); // Required separate method declaration public static bool IsEven...

C#- Delegates vs Events

Image
Delegates and events are closely related concepts in C# and .NET, and they are often used together to implement the Observer design pattern. Here are the key differences between delegates and events, along with their common usage: They allow you to select and transform data from the source collection or query into a new form. Here are some common projection operators Delegates: A delegate is a type that represents references to methods with a specific signature. It acts as a function pointer, allowing you to encapsulate a method and pass it around in your code. Usage: Delegates are used to define and work with method signatures. They are commonly used in scenarios where a callback mechanism is needed, such as in event handling or asynchronous programming. public delegate void MyDelegate(string message); public class MyClass { public void MyMethod(string message) { Console.WriteLine...

Swicth Statement in C#7

New Way of Switch Statement private string GetDefaultMessage(int statusCode) { return statusCode switch { 400 => "Bad Request", 401 => "Unauthorized", 404 => "Not Found", 500 => "Internal Server Error", _ => null }; }