C# 11 Features

In this tutorial, we are going to discuss the new features in C# 11. To begin with,

How to use C# 11.0?

Let us understand the IDE and version requirements to use C# 11.0. In my below article, I have explained the .NET 7.0 Preview version and the installation steps.

The tools which I have used for this tutorial are

  1. Microsoft Visual Studio Community 2022 (64-bit) – Current
  2. Version 17.4.0 Preview 2.1
  3. .NET 7.0
  4. Console App

Let us look at some of my favorite features of C# 11.0

  1. Raw String Literals
  2. Pattern Matching List
  3. New Line in Interpolation
  4. Auto-Default struct
  5. Required Keyword

The source can be downloaded from GitHub

Raw String Literals

Raw string literals can be

  • Multiline
  • Contains quotes
  • Contains special characters without escape sequences.
  • Contains string interpolations.

I have provided a very simple example to explain the difference between Raw String Literals and old approach.

using System.Text;

namespace CSharp11Features.RawLiteralString
{
    internal class DisplayRawStrings
    {
        public static string OldApproach()
        {
            var _rawstring = new StringBuilder();
            _rawstring.Append("I'm explaining \"raw string\" here with escape character");

            return _rawstring.ToString();
        }
        public static string NewApproach()
        {
            var _rawstring = new StringBuilder();
            _rawstring.Append("""I'm explaining "raw string" here without escape character""");

            return _rawstring.ToString();
        }
        public static string InterpolationApproach()
        {
            var firstName = "Prasad";
            var _rawstring = $$"""
                            {
                             "firstName" : "{{firstName}}"

                            }
                            """;

            return _rawstring;
        }
    }
}

The output of the above code will be,

Raw String Literals

Raw literals will be created starts with three or more double quotes and end with three or more double quotes.

Pattern Matching List

The below example provides an overview of List pattern matching

namespace CSharp11Features.PatternMatching
{
    internal class ListPatternExample
    {
        static int[] numbers = { 1, 2, 3 };

        public static bool IsMatch()
        {
            var match = numbers is [1, 2, 3];

            return match;
        }

        public static bool IsNotMatch()
        {
            var match = numbers is [1, 2, 4];

            return match;
        }

        public static bool IsMatchWithExpression()
        {
            var match = numbers is [0 or 1, <=2, >=3];
            return match;
        }
    }
}

The output will be as below

Pattern Matching List

New Line in Interpolation

In C# 11, it will accept a new line in interpolation.

Please take a look at the example below.

namespace CSharp11Features.NewLineInInterpolation
{
    internal class NewLineInInterpolationExample
    {
        public static string NewLineApproach()
        {
            var name = "PRASAD";

            var _rawString = $"Fistname in LowerCase : {name
                .ToLower()}";
            return _rawString;
        }
    }
}

The ToLower() function has been added in the next line. But in previous version it will throw error.

Auto-Default Struct

In the previous C# version, if we are going to create struct as below, it will throw error

namespace CSharp11Features.AutoDefaultStruct
{
    public struct Name
    {
        public string FirstName;
        public string LastName;

        public Name(string FirstName,string LastName) {
        //this.FirstName = FirstName;
        //this.LastName = LastName;
        }
    }
}

The error message would be as below

Auto-Default Struct

Let us update the language version to 11 and see whether the errors are resolved or not. I leave this to the readers to check and confirm. The source code is available on GitHub.

Required Keyword

The required modifier indicates that the property it applied to must be initialized by all constructors or by using an object initializer.

For more details, please refer to the Microsoft documentation.

Please refer to the example below.

Create a class

namespace CSharp11Features.RequiredKeyword
{
    internal class RequiredExample
    {
        public required string FirstName { get;set; }
        public required string LastName { get;init; }
    }
}

Create an object of this class as below

var _required = new RequiredExample()

You will get the compiler error as below

Required Keyword

How this error can be resolved?

Initialize the properties as below

var _required = new RequiredExample{
    FirstName = "Prasad",
    LastName = "Raveendran"
};

We have discussed a few exciting features in C#. There are many more features. Please refer to the below links to know more on additional features.

Thank for you reading my article. Please leave your comments in the comment box below.