Thursday, April 20, 2023

Top 8 New Features in C# 11

C# 11 is the latest version of the C# programming language, released in November 2021. Here are some of the top features included in C# 11:

1. Global using directives:

You can now add a global using directive to your C# files, which means you don't need to add using statements for commonly used namespaces in every file.
// Instead of adding using statements for every file, you can use a global using directive
// to import commonly used namespaces into every file in your project
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Text;
2. Improved target-typed conditional expressions:

C# 11 allows for more flexible use of target-typed conditional expressions, which can make your code more concise and readable.
// C# 11 allows for more flexible use of target-typed conditional expressions
// Here's an example that uses a target-typed conditional expression to check if a value is null
object? nullableObject = null;
string result = nullableObject is null ? "Object is null" : "Object is not null";
Console.WriteLine(result);
3. File-scoped namespaces:

With file-scoped namespaces, you can define a namespace for an entire file rather than having to include a namespace declaration for each individual class.
// With file-scoped namespaces, you can define a namespace for an entire file
// rather than having to include a namespace declaration for each individual class
namespace MyNamespace;

class MyClass
{
    // Class members go here
}
4. Interpolated string improvements:

Interpolated strings now support string interpolation for expressions that return values of any type, not just strings.
// Interpolated strings now support string interpolation for expressions that return values of any type
int x = 5;
string result = $"The value of x is {x}";
Console.WriteLine(result);
5. Extended support for lambda discard parameters:

You can now use discard parameters in lambda expressions, which can make your code more concise and easier to read.
// You can now use discard parameters in lambda expressions
List numbers = new() { 1, 2, 3, 4, 5 };
numbers.ForEach(_ => Console.WriteLine("Hello"));
6. "and" and "or" patterns:

C# 11 introduces new patterns that allow for more complex pattern matching logic, including the ability to combine patterns using "and" and "or".
// C# 11 introduces new patterns that allow for more complex pattern matching logic
// including the ability to combine patterns using "and" and "or"
object obj = "Hello, world!";
if (obj is string { Length: > 5 } or null)
{
    Console.WriteLine("The object is a string with length greater than 5, or null");
}
7. Improved support for global usings in .NET Standard 2.1 and earlier:

Global using directives are now supported in .NET Standard 2.1 and earlier, making it easier to write cross-platform code.
// Global using directives are now supported in .NET Standard 2.1 and earlier
// Here's an example of using a global using directive to import System.IO
// into every file in your project
global using System.IO;
8. Improved support for nullability:

C# 11 includes several improvements to nullability, including better support for nullable reference types and the ability to specify nullability for parameters.
// C# 11 includes several improvements to nullability
// including better support for nullable reference types and the ability to specify nullability for parameters
void MyMethod(string? nullableString)
{
    // The nullableString parameter may be null, so we need to check for null before using it
    if (nullableString is not null)
    {
        Console.WriteLine(nullableString.ToUpper());
    }
}

No comments:

Post a Comment