C# 12: New Features Galore!

C# 12: New Features Galore!

C# 12 arrived in November 2023 alongside .NET 8, packing a punch of new features aimed at simplifying and streamlining your coding experience. Let’s dive into the details of these exciting additions:

1. Primary Constructors:

  • Define concise constructors in any class or struct.
  • Reduces boilerplate code for creating read-only properties.
  • Two flavors: record-style (public read-only) and non-record style (private fields).
  • Example:


public class User
    public string Name { get; }
    public int Age { get; }

    public User(string name, int age)
        Name = name;
        Age = age;

2. Collection Expressions:

  • Build common collections like arrays and lists with a simplified syntax.
  • More concise and readable than traditional initialization.
  • Examples:


// Array with three elements
int[] numbers = { 1, 2, 3 };

// List with five elements squared
List<int> squares = List.of(1, 4, 9, 16, 25);

3. ref readonly parameters:

  • Pass references to variables for modification while ensuring immutability within the method.
  • Improves performance compared to copying values.
  • Useful for efficiently manipulating large data structures.
  • Example:


void Sort(ref readonly int[] data)
    Array.Sort(data); // Modifies original data through reference

4. Default Lambda Parameters:

  • Assign default values to optional parameters in lambda expressions.
  • Enhances code readability and avoids null checks.
  • Example:


List<int> evens = numbers.Where(n => n % 2 == 0 || default); // even is optional with default value null

5. Alias any type:

  • Create semantic aliases for any type using the using alias directive.
  • Improves code readability and clarity, especially for complex types.
  • Example:


using Point = System.Drawing.Point;

Point origin = new Point(0, 0);

6. Inline arrays:

  • Declare and initialize small arrays directly within expressions.
  • Reduces unnecessary variable declarations and improves conciseness.
  • Example:


Console.WriteLine($"Numbers: {new[] { 1, 2, 3 }}");

7. Experimental Features:

  • C# 12 also introduces some exciting experimental features like interceptors for modifying method behavior. These are not yet stable for production use, but offer a glimpse into future possibilities.

Beyond the highlights:

This is just a taste of the new features in C# 12. Other additions include optional parameters in lambda expressions, record struct enhancements, and improved diagnostics. To learn more about each feature in detail, check out the official Microsoft documentation: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12

Whether you’re a seasoned C# developer or just starting out, C# 12 offers some powerful tools to make your code cleaner, more efficient, and enjoyable to write. Give these new features a try and see how they can elevate your coding game!

Leave a Reply

Your email address will not be published. Required fields are marked *