What Is The C# 12 Preview Features

Introduction

C# 12 Preview Features: A Sneak Peek into the Future of Programming

C# is a popular programming language developed by Microsoft, widely used in developing desktop, web, and mobile applications. With each new release, C# brings exciting features and improvements, making it more powerful, expressive, and productive. The latest version of C# is C# 10, released in 2021, which introduced a range of new features, such as global using directives, interpolated strings, and file-scoped namespaces, to name a few. However, Microsoft is already working on the next version of C#, C# 12, which promises to bring even more exciting features and improvements to the language.

In this article, we will take a sneak peek into some of the new features currently in preview for C# 12. Below are the major components, but this article will explain a few. 

  1. Record structs
  2. Support for global using directives
  3. Interpolated strings as format strings
  4. Lambda support for method-like delegate types
  5. Improvements to the static keyword for top-level programs
  6. Simplified parameter null checking
  7. Support for anonymous record types
  8. Improvements to the nameof operator.
  9. Improved Switch Expressions

Improved Switch Expressions

Switch expressions were introduced in C# 8, allowing developers to express complex conditional logic concisely and readably. C# 12 takes this further by introducing a new pattern-matching syntax for switch expressions, making writing expressive and concise code even more accessible.

Consider the following code snippet.

var result = obj switch
{
    int i when i > 0 => "Positive",
    int i when i < 0 => "Negative",
    _ => "Zero"
};

This code uses switch expressions to determine whether an integer is positive, negative, or zero. When a keyword is used to introduce a pattern that must match for the corresponding case to be executed. In C# 12, we can simplify this code even further using the new pattern-matching syntax,

var result = obj switch
{
    > 0 => "Positive",
    < 0 => "Negative",
    _ => "Zero"
};

This syntax allows us to omit the when keyword and use relational operators directly in the switch expression.

Property and Method Improvements

C# 12 introduces several improvements to properties and methods, making them more expressive and flexible.

Property Initializers

Property initializers allow us to set default values for properties when they are declared. In C# 12, we can now use constructors to initialize properties, making it even easier to set up complex objects.

class MyClass
{
    public int MyProperty { get; set; } = 42;

    public MyClass()
    {
        // Other constructor code
    }
}

Method Extensions

Method extensions allow us to add new methods to existing classes without modifying the class. In C# 12, we can now use extension methods to add methods to interfaces, making it easier to write reusable code.

interface MyInterface
{
    void Method1();
}

static class MyExtensions
{
    public static void Method2(this MyInterface obj)
    {
        // Method implementation
    }
}

class MyClass: MyInterface
{
    public void Method1()
    {
        // Method implementation
    }
}

var obj = new MyClass();
obj.Method1();
obj.Method2();

Lambda Improvements

Lambdas are a powerful feature in C#, allowing us to write concise and expressive code. In C# 12, lambdas get even better by introducing several improvements.

1. Implicit Typed Lambdas

In C# 12, we can now use implicit typing in lambdas, making them even more concise and readable.

List<int> numbers = new() { 1, 2, 3, 4, 5 };

var evenNumbers = numbers.Where(n => n % 2 == 0);

In this code, we use an implicit typed lambda expression

Simplified parameter null checking

Sure, here's an example of simplified parameter null checking using C# 12 preview's new feature of parameter null checking:

public void ExampleMethod(string? parameter1, string? parameter2)
{
    // Parameter null checking using the new '!' operator
    parameter1! ?? throw new ArgumentNullException(nameof(parameter1));
    parameter2! ?? throw new ArgumentNullException(nameof(parameter2));

    // Rest of the method logic goes here...
}

In the above example, the ! operator is used to null-check the parameter1 and parameter2 parameters before proceeding with the rest of the method logic. If either parameter is null, an ArgumentNullException is thrown with the parameter name as the message.

Note. that the "!" operator is used after the parameter name, which tells the compiler that the parameter is not null at this point in the code. If the parameter is null, the throw statement is executed, and the method exits immediately without executing any further logic.

Summary

This new feature helps simplify parameter null checking in C# and reduces the boilerplate code needed to ensure that parameters are not null.

So hopefully, we have learned many of the features of the C# 12 version in this article.


Similar Articles