Posts

Showing posts with the label #csharp

C# 10 & 11: Unleash the Power of Records, Global Usings, Extended Patterns, and More!

Image
  Here are 5 of the latest C# tips, along with code examples: 1. Record Types (C# 10): Create lightweight, immutable data structures with concise syntax. Useful for representing data without the overhead of full-fledged classes. C# record Point ( int X , int Y ); Point p = new Point( 5 , 10 ); // Create a record instance Console.WriteLine(p.X); // Access record members 2. Global Using Directives (C# 10): Reduce repetitive using statements in multiple files. Improve code readability and maintainability. C# // In a global using file: global using System.Collections.Generic; global using System.Linq; // In other files: List< int > numbers = new List< int >(); int sum = numbers.Sum(); // No need for local using statements 3. Extended Property Patterns (C# 11): Match and deconstruct properties for more flexible pattern matching. Useful for data extraction and transformation. C# Point p = new Point( 5 , 10 ); if (p is { X: var x,