Fatih Kacar
Published on
11/17/2023 09:01 am

What's New in C# 12: Primary Constructors, Collection Expressions, and More

Authors
  • Name
    Fatih Kacar
    Twitter

Discovering the Exciting Updates of C# 12: From Primary Constructors to Collection Expressions

As part of the .NET 8 launch, on November 14th, Microsoft unleashed a wave of excitement among developers with the introduction of C# 12, the newest version of the highly acclaimed .NET programming language. Packed with a plethora of innovative features, this release marks a significant milestone in the evolution of C#.

Revolutionizing Code Construction with Primary Constructors

C# 12 introduces the revolutionary concept of primary constructors for all classes and structs. This feature simplifies the process of declaring and initializing properties, making code more concise and readable. With primary constructors, you can now define properties directly within the class declaration, eliminating the need for additional constructor methods.

class Person
{
    public string Name { get; init; }
    public int Age { get; init; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

The primary constructor allows you to initialize properties as soon as an instance is created, resulting in cleaner and more efficient code. Furthermore, this enhancement facilitates the implementation of immutable classes, as the "init" keyword restricts modification of the properties after initialization.

Better Collection Manipulation with Collection Expressions

Collection manipulation has never been easier thanks to the introduction of collection expressions in C# 12. This new feature allows you to initialize collections directly within the declaration, eliminating the need for separate initialization statements.

var fruits = new List<string> { "Apple", "Banana", "Orange" };

With collection expressions, you can conveniently add, remove, or modify elements of lists, arrays, dictionaries, and other collection types in a concise and intuitive manner. This improvement boosts productivity and enhances code readability, enabling developers to work more effectively with complex data structures.

Enhanced Flexibility with Type Aliases

C# 12 introduces a new syntax that allows developers to define type aliases, providing greater flexibility and readability in code. Type aliases enable you to create alternative names for existing types, simplifying complex type declarations and making code more self-explanatory.

using MyInt = System.Int32;

MyInt age = 30;

In the example above, the "MyInt" alias is defined for the "System.Int32" type. This enables you to use "MyInt" as a substitute for "Int32" throughout the codebase, enhancing code clarity and reducing the likelihood of errors.

Default Parameters for Lambda Expressions

Prior to C# 12, specifying default parameters was only possible for methods. Now, with the introduction of default parameters for lambda expressions, you can define optional arguments when working with delegate types. This feature enhances the flexibility of lambda expressions and simplifies their usage in various scenarios.

Func<int, int, int> add = (a, b = 0) => a + b;
int result1 = add(5, 3); // result1 = 8
int result2 = add(5);    // result2 = 5

In the example above, the lambda expression "add" accepts two parameters, with the second parameter having a default value of 0. This allows you to call the lambda expression with a single argument, utilizing the default value for the second parameter if not explicitly provided.

Conclusion

The release of C# 12 brings a plethora of exciting features and enhancements, revolutionizing the way developers write code within the .NET ecosystem. With primary constructors, collection expressions, type aliases, and default parameters for lambdas, C# becomes more expressive, concise, and flexible.

As a developer, embracing these updates will unlock new possibilities, enhance productivity, and elevate the quality of your codebase. Whether you are creating robust applications or exploring new avenues of software development, C# 12 equips you with the tools to thrive in the ever-evolving world of programming.

So, dive into C# 12, embrace the future of .NET development, and unleash your creativity!

By Almir Vuk