C# 11 is the latest version of the C# programming language that was released in 2022 as part of .NET 7. It introduces many new features and enhancements that make C# more expressive, concise, and powerful. In this article, we will explore some of the highlights of C# 11 and show you how to use them with some code examples.

Generic attributes
You can now declare generic classes that inherit from System.Attribute and use them as custom attributes. This allows you to create attributes that take a type parameter and apply them without using the typeof operator. For example, you can write:
public class GenericAttribute<T> : Attribute { }
[GenericAttribute<string>]
public string Method() => default;
Generic math
You can now perform mathematical operations on generic types that implement certain interfaces, such as INumber<T>, IAdditionOperators<T>, etc. This enables you to write generic algorithms that work with any number-like type. For example, you can write:
public static T Add<T>(T a, T b) where T : INumber<T>
{
return a + b;
}
Raw string literals
You can now write string literals that span multiple lines and contain any characters without escaping. This is useful for writing regular expressions, SQL queries, HTML templates, etc. For example, you can write:
string raw = @"This is a raw string literal
that can contain ""quotes""
and \backslashes\
without escaping.";
UTF-8 string literals
You can now write string literals that are encoded in UTF-8 instead of UTF-16. This can improve performance and memory usage when working with text data that is mostly ASCII. For example, you can write:
Utf8String utf8 = u8"This is a UTF-8 string literal";
Newlines in string interpolation expressions
You can now write string interpolation expressions that span multiple lines without using parentheses. This makes the code more readable and consistent with other expressions. For example, you can write:
string message = $"Hello,
{name},
how are you?";
These are just some of the new features and enhancements in C# 11. For more details, you can check out the official documentation or the blog post. C# 11 is a modern and versatile programming language that offers many benefits for developers. You can try it out today by downloading .NET 7 and Visual Studio 2022.
Disclaimer
The articles on this demo website were created with the assistance of Microsoft Bing search for demonstration purposes only. The content may not be accurate and is not intended to represent the views or opinions of any individual or organization.