C# 12: The New Features That You Need to Know

Introduction: 

C# 12 is the latest version of the C# programming language, and it includes a number of new features that can make your code more concise, readable, and efficient. In this blog post, we will take a look at some of the most important new features in C# 12.


Primary constructors for non-record classes and structs:
 

One of the most significant new features in C# 12 is the ability to create primary constructors for non-record classes and structs. Primary constructors are constructors that are used to initialize the properties of a class or struct. In previous versions of C#, primary constructors were only available for record types. This meant that you had to use the default constructor to initialize the properties of non-record types, which could be cumbersome and error-prone.
 

C# 12 makes it much easier to initialize the properties of non-record types by allowing you to use primary constructors. This can save you time and effort, and it can also make your code more readable and maintainable.
 

Here is an example of how to use primary constructors for non-record classes and structs:

C#

// This code shows how to use primary constructors for non-record classes and structs.


class Person {

    public string FirstName { get; set; }

    public string LastName { get; set; }


    public Person(string firstName, string lastName) {

        FirstName = firstName;

        LastName = lastName;

    }

}


struct Point {

    public int X { get; set; }

    public int Y { get; set; }


    public Point(int x, int y) {

        X = x;

        Y = y;

    }

}


Optional parameters in lambda expressions:

Here is an example of how to use optional parameters in lambda expressions:

C#

// This code shows how to use optional parameters in lambda expressions.


var people = new List<Person>();

people.Add(new Person("John", "Doe"));

people.Add(new Person("Jane", "Doe"));


var filter = (person) => person.FirstName.StartsWith("J");


var john = people.First(filter);

var jane = people.FirstOrDefault(filter);


Alias any type:

C# 12 also introduces the ability to alias any type, not just named types. 

This can be useful for making code more readable and maintainable.

Here is an example of how to alias any type:

C#

// This code shows how to alias any type.


using text = string;


var text1 = "Hello, world!";

var text2 = "Goodbye, world!";


Inline arrays:

C# 12 allows you to create inline arrays within structs. 

This can be useful for improving the performance of your code.

Here is an example of how to create inline arrays within structs:

C#

// This code shows how to create inline arrays within structs.


struct Point2 {

    public int[] Coordinates { get; set; }


    public Point2(int x, int y) {

        Coordinates = new int[] { x, y };

    }

}


var point2 = new Point2(10, 20);


Interceptors:

C# 12 introduces an experimental feature called interceptors. Interceptors allow you to intercept
code at runtime. This can be useful for debugging, performance optimization, and other purposes.
Here is an example of how to use interceptors:

 

C#

// This code shows how to use interceptors.


class LoggingInterceptor : IInterceptor {

    public void Intercept(IInvocation invocation) {

        Console.WriteLine("Intercepting call to {0}", invocation.Method.Name);

        invocation.Proceed();

    }

}


var interceptor = new LoggingInterceptor();

var logger = new Logger(interceptor);


logger.Log("Hello, world!");


Sources

1. https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references

Post a Comment

Previous Post Next Post