What's New In C# 11?

C# continues to evolve, and with the release of C# 11, developers are introduced to a plethora of new features and enhancements designed to improve productivity, code readability, and performance. In this blog post, we’ll delve into some of the most notable features in C# 11, providing insights and examples on how they can be leveraged in your applications.

1. Raw String Literals

C# 11 introduces raw string literals, which make it easier to work with multi-line strings and strings containing escape sequences. This feature simplifies the creation and readability of strings, particularly those used for JSON, XML, or SQL queries.

Example

string json = """
{
    "name": "John Doe",
    "age": 30,
    "isDeveloper": true
}
""";
Console.WriteLine(json);

The triple quotes (""") allow you to write strings without needing to escape special characters or manage line breaks manually.

2. List Patterns

List patterns enable pattern matching on arrays and lists. This feature allows you to deconstruct and match elements within a collection, providing more expressive and concise code for handling complex data structures.

Example

void ProcessNumbers(int[] numbers)
{
    switch (numbers)
    {
        case [1, 2, 3]:
            Console.WriteLine("Matched: [1, 2, 3]");
            break;
        case [_, 4, _]:
            Console.WriteLine("Matched: [_, 4, _]");
            break;
        case [var first, .., var last]:
            Console.WriteLine($"First: {first}, Last: {last}");
            break;
        default:
            Console.WriteLine("No match");
            break;
    }
}

ProcessNumbers(new int[] { 1, 2, 3 });
ProcessNumbers(new int[] { 5, 4, 6 });
ProcessNumbers(new int[] { 7, 8, 9, 10 });

In this example, list patterns simplify the process of matching and extracting values from arrays.

3. Required Members

Required members enforce that certain properties must be initialized either during object creation or in the constructor. This feature helps ensure that objects are always in a valid state.

Example

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

var person = new Person
{
    FirstName = "Jane",
    LastName = "Doe",
    Age = 28
};

In this example, FirstName and LastName are required properties, ensuring they are always set when creating an instance of the Person class.

4. UTF-8 String Literals

C# 11 introduces native support for UTF-8 string literals, which can improve performance by reducing the need for encoding conversions. This is particularly beneficial for applications that handle a lot of text processing.

Example

ReadOnlySpan<byte> utf8Greeting = "Hello, world!"u8;
Console.WriteLine(Encoding.UTF8.GetString(utf8Greeting));` 

The u8 suffix creates a UTF-8 encoded byte array directly from the string literal.

5. File-Scoped Types

File-scoped types restrict the visibility of a type to the file in which it is declared. This can help reduce naming conflicts and improve code organization.

Example


file class InternalHelper
{
    public static void Assist() => Console.WriteLine("Assisting...");
}

// This class is only accessible within this file.
InternalHelper.Assist();

In this example, InternalHelper is only accessible within the file it is declared in, preventing unintended usage elsewhere in the project.

6. Generic Attributes

C# 11 allows attributes to be generic, providing greater flexibility and type safety in scenarios where attributes are used.

Example


[AttributeUsage(AttributeTargets.Class)]
public class ServiceAttribute<T> : Attribute
{
    public Type ServiceType => typeof(T);
}

[Service<MyService>]
public class MyServiceConsumer
{
    // Implementation
}

Console.WriteLine(new ServiceAttribute<MyService>().ServiceType);

Generic attributes can simplify attribute definitions and enhance type safety.

Conclusion

C# 11 introduces several powerful features that enhance the language’s expressiveness and capabilities. From raw string literals and list patterns to required members and UTF-8 string literals, these features enable developers to write cleaner, more efficient code. By understanding and adopting these new features, you can take full advantage of the improvements in C# 11 and continue to build robust, maintainable applications.

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post