C#

C# 12 Features: What’s New and Improved in the Latest Release


In this tutorial we will learn about C#12 new features those can use useful for developers.

C#12 comes with .NET 8.0 SDK and VS 2022

We have below new features in C#12

1. Primary Constructors

2. Collection Literals

3. Default Lambda Parameter Values

4. Required members,

5. New Switch Statement


Let's Discuss in details

Primary Constructors for Classes

Primary constructors allow you to define a class constructor directly in the class declaration. This simplifies class definitions by combining constructor parameters and property initializations.

Before C#12


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

In C#12



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


In C#12 we can see , class itself can take parameters which is nothing but primary constructor.


 Collection Literals

Collection literals provide a concise syntax to initialize collections, making the code more readable and less error-prone


//Before C# 12 var numbers = new List<int> { 1, 2, 3, 4, 5 }; var dictionary = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; //In C#12 var numbers = [1, 2, 3, 4, 5]; var dictionary = ["key1": "value1", "key2": "value2"];

More example


//in C#12 // Create a list: List<string> b = ["one", "two", "three"];


Default Lambda Parameter Values

Default lambda parameter values allow lambdas to have default values for their parameters, increasing flexibility and reducing the need for method overloads.


//before C#12 Func<int, int, int> add = (x, y) => x + y; //In C#12 Func<int, int, int> add = (x = 0, y = 0) => x + y;


More examples of default parameter in lambda expressions


Example with default int value Func<int, int, int> add = (x = 0, y = 0) => x + y; // Directly use with default parameters int result1 = add(); // 0 + 0 = 0 int result2 = add(5); // 5 + 0 = 5 int result3 = add(5, 10); // 5 + 10 = 15 Example with default string value Func<string, string, string> greet = (greeting = "Hello", name) => $"{greeting}, {name}!"; // Directly use with default parameters string message1 = greet("Hi", "devesh"); // "Hi, devesh!" string message2 = greet("gupta"); // "Hello, gupta!"


Required Members

The required keyword enforces the initialization of certain properties during object construction, ensuring critical properties are always set.

This will Ensures that essential properties are initialized, reducing runtime errors.

Before C#12


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


In C#12


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


Enhanced switch Expressions

Enhanced switch expressions support pattern matching and more expressive syntax, making the code cleaner and more readable.

Before C#12



string result; if (expression is int i) { if (i > 0) result = "Positive"; else if (i < 0) result = "Negative"; else result = "Zero"; }


In C#12



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


line

Copyrights © 2024 letsupdateskills All rights reserved