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.
  1. // Instead of adding using statements for every file, you can use a global using directive
  2. // to import commonly used namespaces into every file in your project
  3. global using System;
  4. global using System.Collections.Generic;
  5. global using System.Linq;
  6. 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.
  1. // C# 11 allows for more flexible use of target-typed conditional expressions
  2. // Here's an example that uses a target-typed conditional expression to check if a value is null
  3. object? nullableObject = null;
  4. string result = nullableObject is null ? "Object is null" : "Object is not null";
  5. 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.
  1. // With file-scoped namespaces, you can define a namespace for an entire file
  2. // rather than having to include a namespace declaration for each individual class
  3. namespace MyNamespace;
  4.  
  5. class MyClass
  6. {
  7. // Class members go here
  8. }
4. Interpolated string improvements:

Interpolated strings now support string interpolation for expressions that return values of any type, not just strings.
  1. // Interpolated strings now support string interpolation for expressions that return values of any type
  2. int x = 5;
  3. string result = $"The value of x is {x}";
  4. 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.
  1. // You can now use discard parameters in lambda expressions
  2. List numbers = new() { 1, 2, 3, 4, 5 };
  3. 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".
  1. // C# 11 introduces new patterns that allow for more complex pattern matching logic
  2. // including the ability to combine patterns using "and" and "or"
  3. object obj = "Hello, world!";
  4. if (obj is string { Length: > 5 } or null)
  5. {
  6. Console.WriteLine("The object is a string with length greater than 5, or null");
  7. }
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.
  1. // Global using directives are now supported in .NET Standard 2.1 and earlier
  2. // Here's an example of using a global using directive to import System.IO
  3. // into every file in your project
  4. 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.
  1. // C# 11 includes several improvements to nullability
  2. // including better support for nullable reference types and the ability to specify nullability for parameters
  3. void MyMethod(string? nullableString)
  4. {
  5. // The nullableString parameter may be null, so we need to check for null before using it
  6. if (nullableString is not null)
  7. {
  8. Console.WriteLine(nullableString.ToUpper());
  9. }
  10. }

No comments:

Post a Comment