C# 11 features I like the most

It has been a while since I posted my previous blog post. I finally made a schedule for me to put more effort into writing and slowly get back to the speed which I was following before few years. Feel free to reach out if you want me to write on certain topics.

In this post, I will list down the top 5 C# 11 features that I like the most.

C# 11 is the latest version of the popular programming language, and it comes with a number of exciting new features and improvements that help developers write cleaner, more efficient code.

So let us start.

File-scoped namespaces

One of the most significant changes in C# 11 is the introduction of file-scoped namespaces. Previously, developers had to define their namespaces at the top of every file, which could lead to cluttered and confusing code. With file-scoped namespaces, you can define your namespace at the top of your file, and all the code in that file will belong to that namespace. For example:

namespace MyNamespace;

public class MyClass
{
    // Class code here
}

This can help simplify your code and make it more organized.

Improved Interpolated Strings

C# 11 also introduces improved interpolated strings. Interpolated strings allow you to embed expressions inside a string literal, making it easier to format your output.

Interpolated strings are a powerful tool for formatting strings in C#, and with the improvements introduced in C# 11, developers can now use them as format strings for string.Format method calls. For example:

string name = "John";
int age = 30;

string message = string.Format($"My name is {name} and I am {age} years old.");

This can help make your code more concise and readable.

and, or, and not operators in pattern matching

Pattern matching in C# 11 has been enhanced with the introduction of the and, or, and not operators. These operators allow you to combine patterns to make more complex pattern matches. For example:

if (obj is string s and { Length: > 5 } and not null)
{
    Console.WriteLine($"The string is '{s}' and has more than 5 characters.");
}
else if (obj is int i and not 0)
{
    Console.WriteLine($"The int is {i} and is not equal to 0.");
}
else
{
    Console.WriteLine("Unknown type");
}

This can be especially useful when working with complex data structures, such as trees or graphs.

Global using directives

C# 11 introduces global using directives, which allow you to define a set of using directives that apply to all files in your project. This can help reduce boilerplate code and make it easier to work with third-party libraries. For example:

global using System;
global using System.Linq;
global using System.Collections.Generic;

This can help simplify your code and reduce the number of lines you need to write.

Lambda discard parameters

C# 11 introduces the ability to use a discard parameter in a lambda expression. A discard parameter is essentially a variable that is ignored, meaning it can be used to write more concise code. For example:

// Using a discard parameter in a lambda expression
_ = myNumbers.Select(_ => Math.Sqrt(_));

This code will execute the Math.Sqrt() method on each element in myNumbers, but instead of creating a variable to store the result, we use a discard parameter to indicate that we’re not interested in the value. This can help reduce clutter in your code and make it more concise.

In conclusion, the new features of C# 11 offers a range of benefits to developers, including simplifying your code, making it more expressive, and reducing boilerplate code.

By taking advantage of these features, you can write cleaner, more efficient code and improve your productivity as a developer. I encourage you to try out these features and discover the full potential of C# 11.

What is your favorite C# 11 feature? Mention that in the comment section 🙂

I hope it helps. Happy learning.

5 thoughts on “C# 11 features I like the most

  1. Using the discard in your example does not appear to be the right usage, even though it’s syntactically correct. The value may not matter to you, but it matters to the code in your example. The discard, at least from my understanding, is meant to convey the intent that the value does not matter to both the code (e.g. unused parameter or unused result of a method) and the developer.

    Like

    1. Thanks for your comment and you are right about the use of a discard parameter should convey the intent that the value does not matter to both the code and the developer.

      In the example I provided, the Select method is being used to transform a collection of numbers by applying the Math.Sqrt method to each number. The result of this transformation is not being assigned to a variable or used in any other way. In this case, the value returned by the Math.Sqrt method is not relevant to the code or the developer, and using a discard parameter can make the code more concise and readable.

      Like

      1. Fair enough. I share your likes. Especially with the pattern usage and file-scoped namespaces. The latter is awesome — anything to reduce horizontal scrolling’s got to be a win in general. Cheers.

        Liked by 1 person

Leave a comment