New C# Features every developer should know

Charles
4 min readSep 11, 2023

C# is always evolving and in its 11th version as at writing. Many features has been introduced and improved, and today I am going to discuss about the new C# features every developer should know.

Image by 8photo on Freepik

1. The Raw string literal

The raw string literal is a new feature in C# 11 that does string formatting in a new way by starting and ending the string with a three double quote. In previous version of c#, this is only possible by using string concatenation or the @ sign.

Using the “+” or “@” sign can make coding difficult because you have to be observing where to put the “+” sign or how to do escaping. With the raw string literal feature, you don’t have to worry about that because it is easy to start writing strings in-between the three double quote that support the “string interpolation” and you can also define “quotes” within it.

The boring string concatenation

public class Traning
{
public static void Main()
{
string desc = "my name is john doe."
+"I am a software engineer."
+"I love c#";
Console.WriteLine(desc);
}
}

The new way with Raw String literals

public class Traning
{
public static void Main()
{
string desc = """
my name is john doe.
I am a software engineer.
I love c#
""";
Console.WriteLine(desc);
}
}

You can also do string interpolation and add quotes inside the raw string literal

string end = ".";
string desc = $"""
my name is john doe.
I am a software engineer.
"I love c#{end}"
""";

2. Local functions

Have you ever imagined declaring a method inside a method? This is a new feature in C# that makes your coding succinct. If your method is too long and tedious, the local function may help to make it more understandable and maintainable.

public string Statistics(params int[] nums)
{
int Mean(){ return nums.Sum()/nums.Count(); }
double Average(){ return nums.Average() }
return $"The mean is-{Mean()} and the average is-{Average()}";
}

The local function is very useful and it makes your method cleaner and easy for developers to read.

3. List patterns

In C# 11, you can match an array or list against a sequence, you can also use conditionals to do pattern matching. For example, num is [1,2,3] returns true if num array is actually [1,2,3]. The list pattern also support using the range “..” to match zero or more elements or a discard for a single element

int[] num = {1,2,3}
bool check = nums is [1,2,3]; //returns true
bool check2 = nums is [1,..] //.. gets the rest elements and returns true
bool check3 = nums is [0 or 1, ..] // the first element is 0 or 1 and ".." returns the rest

4. Records

Records is another cool feature in c#. It is a reference type that is used to encapsulate data. It is also recommended to use records as DTO’s and it also has a built in format for displaying data. Records may not be new but there are many things you can do with it for example, using the required keyword and init-only setters along with records to do better validations.

Basic record declaration

public record Profile(string Fname, string Lname);  
var p = new Profile("John","Doe");
Console.WriteLine(p); //returns Profile { Fname = John, Lname = Doe }
//can also do p.Fname

Records can also have properties and if you notice the code below you can see that the “Fname” variable is declared as a required property which simply means that the property must be set. Don’t confuse the required keyword from data annotations in the System.ComponentModel namespace, it is different. The required enables you to mark a property as required so that it must be assigned in code. If the property is not assigned, then the code won’t execute. This is very useful especially for validation purposes.

Note: You can also define an init only setters for a advanced property validation.

public record Profile
{
public required string Fname { get; set; }
public string Lname { get; set; }
};

var p = new Profile
{
Fname="John", //must be set because it is required
Lname="Doe"
};

Console.WriteLine(p);

5. Switch Expressions

The new c# switch expression is a mind-blowing feature that every developer must use. In the past defining switch can be tedious and boring but the new switch expression is solving that problem by allowing you to write switch faster.

Level = (maths_score, english_score) switch
{
( >= 50, >= 50) => nameof(ClassLevel.First),
( < 50, < 50) => nameof(ClassLevel.Second),
_ => throw new NotImplementedException()
};

Another cool features you should check out is the Guard clauses, which is coming in .NET 8. Guard clauses enable you to do early error checking in your code, you may never have to write try…catch statement again.

If you’re intrested in learning ASP.NET Core 8 and all its amazing new features, enroll for this course using this link

If you’re interested in learning about software architectures like Clean Architecture and Domain-Driven Design in .NET, check out my stories or enrol in my Udemy course on Software Architectures from the ground up, accessible through this link.

If you’re also interested in learning React, NextJs and Typescript for building rich web applications, watch this Udemy course

Cheers!

--

--