C# 11 Features with Example

In this article, we will explore the C# 11 features with the code snippet. C# 11 was released with .NET in November 2022.

c# 11 Features with code example

Below are the features of C# 11

Interpolated string improvements

C# 11 introduced two new interpolated string features. The first feature allows you to specify the culture that should be used when formatting numeric or date/time values in an interpolated string. The second feature allows you to suppress the format string entirely by using a caret (^) character before the opening brace ({) of an interpolated expression.

Example –

static void Main(string[] args)
        {
            // Interpolated string with culture specifier
            var price = 10.99m;
            var formattedPrice = $"{price:c2}";
            Console.WriteLine(formattedPrice); // output: $10.99

            var cultureInfo = CultureInfo.GetCultureInfo("de-DE");
            formattedPrice = $"{price.ToString("C2", cultureInfo)}";
            Console.WriteLine(formattedPrice); // output: 10,99 €

            // Interpolated string with caret (^) to suppress format string
            var name = "Ramesh";
            var greeting = $"Hello, ^{name}!";
            Console.WriteLine(greeting); // output: Hello, Ramesh!
        }

Top-level statements

This feature allows you to write C# code without needing to wrap it in a class or a method. Top-level statements are executed in the order they appear in the file and can be used for simple programs, scripts, or one-off tools.

Example

using System;

Console.WriteLine("Program execution started...");

// Define some variables
var name = "Suresh";
var age = 30;

// Use the variables in an interpolated string
Console.WriteLine($"Hello, {name}! You are {age} years old.");

// Define a simple function
int Add(int x, int y) => x + y;

// Call the function and print the result
Console.WriteLine($"The sum of 2 and 3 is {Add(2, 3)}.");

Console.WriteLine("Program Execution Completed.");

File-scoped namespaces

File-scoped feature in C# 11 allows you to define a namespace that applies to an entire source file, rather than just a specific class or namespace. This can simplify namespace management and improve code readability.

Example

using System;

namespace MyNamespace;

Console.WriteLine("Program execution started...");

// Define a class
class MyClass
{
    // Class implementation
}

// Use the class in the main method
var myObject = new MyClass();
Console.WriteLine($"Created a new instance of {nameof(MyClass)}.");

// File-scoped namespace for helper functions
namespace MyNamespace.Helpers
{
    // Define a helper function
    int Double(int x) => x * 5;
}

// Use the helper function in the main method
var number = 5;
var doubledNumber = MyNamespace.Helpers.Double(number);
Console.WriteLine($"The value of {number} doubled is {doubledNumber}.");

Console.WriteLine("Program execution completed.");

Lambda improvements

C# 11 introduced two new lambda features. The first feature allows you to use the underscore (_) character as a discard parameter in a lambda expression. The second feature allows you to omit the parameter list entirely when defining a lambda expression that takes no arguments.

Global using directives

This feature allows you to specify using directives that apply to all files in a project, without needing to specify them in each file individually. This can simplify code and improve code readability.

Example

global using System;

Console.WriteLine("Starting the program...");

// Use types from System namespace
var today = DateTime.Today;
var random = new Random();

Console.WriteLine($"Today is {today.ToShortDateString()}.");
Console.WriteLine($"Random number: {random.Next()}.");

 

In this example, we used a global using directive to bring in the System namespace to the entire source file. We then use types from the System namespace, such as DateTime and Random, without needing to include a using directive in each individual file or namespace.

Please follow and like us:

Leave a Comment