Accessible as part of .NET 10, which was launched final November, C# 14 brings a plethora of recent options and enhancements that make it simpler to jot down environment friendly, excessive performant code. Simply as we walked by means of the brand new options and enhancements in C# 13 and C# 12, on this article we’ll take an in depth have a look at a number of the finest new options in C# 14.
To work with the code examples supplied on this article, it is best to have Visible Studio 2026 or a later model put in in your system. Should you don’t have already got a replica, you’ll be able to obtain Visible Studio 2026 right here.
File-based apps
Help for file-based apps is maybe probably the most putting new characteristic on this launch of the C# programming language. Till C# 14, we’ve needed to comply with a multi-step course of to run a minimal .cs file. Not solely was this a multi-step course of, nevertheless it incurred important further overhead since you needed to create an answer file and a venture file to run your software. Even when all you needed to do was carry out a fast calculation or course of a chunk of information rapidly to check your code, you needed to create further recordsdata chances are you’ll not want later. Not.
With C# 14, now you’ll be able to run a C# file instantly from the command line without having a venture or resolution file.
Allow us to perceive this with a code instance. Take into account a file named Demo.cs that incorporates the next code.
Console.WriteLine("This can be a pattern textual content");
DateTime dateTime = DateTime.UtcNow.Date;
Console.WriteLine($"At this time's date is: {dateTime.ToString("d")}");
You possibly can execute this system utilizing the next command on the console window.
dotnet run Demo.cs
When this system is executed, you’ll see the next textual content displayed on the console.

Foundry
Observe you can create file-based apps that reference NuGet packages and SDKs utilizing preprocessor directives, without having a venture or resolution file.
Extension members
Extension members are a brand new characteristic in C# 14 that allow you to declare extension properties in addition to extension strategies. As well as, extension members make it simpler to declare extension strategies than in earlier variations of C#. Earlier than we dive into extension members, let’s first perceive extension strategies.
Within the C# programming language, extension strategies are a characteristic that lets you increase the capabilities of courses with out the need of inheritance. You do not want to create subclasses to make use of extension strategies, neither is it mandatory to change or recompile current class definitions. Along with enhancing code readability, extension strategies allow you to add new strategies to your current sorts (i.e., courses, structs, information, or interfaces). By the way, extension strategies had been first carried out in C# 3.0.
There are quite a few extension strategies in .NET that permit you to develop the querying capabilities of each System.Collections.IEnumerable and System.Collections.Generic.IEnumerable by utilizing the LINQ customary question operator. Whilst you can reap the benefits of extension strategies to increase a category or an interface in C#, you can’t override their strategies. Extension strategies might help you to increase the performance of sorts even when they’re sealed, such because the String class in C#.
For instance, the the place() extension methodology is outlined within the Enumerable static class pertaining to the System.Linq namespace. The next code snippet creates an occasion of the the place() extension methodology:
public static IEnumerable The place(
this IEnumerable supply,
Func predicate)
Observe using the this key phrase. Previous to C# 14, to implement an extension methodology, you needed to create a static methodology and move the this reference as a parameter to the strategy. In C# 14, the code snippet above could be changed utilizing an extension block, with out the necessity of specifying the this parameter. That is proven within the code snippet given beneath.
extension(IEnumerable supply)
{
public IEnumerable
The place(Func predicate)
}
The flexibility to outline extension members has different benefits as effectively. Observe that an extension member requires two kinds of data, i.e., the receiver to which the member needs to be utilized and any parameters it’d want if the member is a technique. With the brand new extension member syntax, you’ll be able to outline an extension block after which write the receivers as wanted. Most significantly, this new syntax allows you to outline a receiver on your extension member that doesn’t require any parameter, i.e., for those who’re utilizing an extension property.
Moreover, by utilizing the brand new syntax, you’ll be able to logically group extensions that apply to the identical receiver. You possibly can then outline a brand new extension block if the receiver adjustments. Furthermore, the static class by which you write your extension blocks or extension strategies (for those who’re utilizing an earlier model of the C# language) can include each the extension strategies that require the this parameter and the extension members grouped inside extension blocks, as proven within the C# 14 code itemizing given beneath.
public static class StringExtensions
{
extension(string worth)
{
public bool ContainsAnyDigit()
{
if (string.IsNullOrEmpty(worth))
return false;
return worth.Any(char.IsDigit);
}
public bool ContainsAnySpecialCharacter()
{
if (string.IsNullOrEmpty(worth))
return false;
return worth.Any(c => !char.IsLetterOrDigit(c));
}
}
public static bool IsNullOrEmptyOrWhiteSpace(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
}
Within the previous code snippet, the extension methodology IsNullOrEmptyOrWhiteSpace makes use of the legacy syntax (i.e., it requires the this parameter), whereas the extension strategies ContainsAnyDigit and ContainsAnySpecialCharacter use the brand new syntax.
You possibly can learn extra about extension members in C# 14 right here.
Enhancements to the nameof operator for unbound generics
C# 14 brings enhancements to the nameof key phrase by supporting unbound generic sorts (e.g., Checklist, Dictionary). Now that nameof can take an unbound generic sort as an argument, you not must outline dummy sort arguments (similar to Checklist) merely to acquire the sort title “Checklist.”
Allow us to perceive this with a code instance. Within the following piece of code, you’ll must specify the sort argument for the solid to work completely.
string typeNameList = nameof(Checklist);
string typeNameDictionary = nameof(Dictionary);
With C# 14, unbound generics work instantly. You not must specify the sort explicitly, as proven within the code snippet given beneath.
string typeNameList = nameof(Checklist);
string typeNameDictionary = nameof(Dictionary);
Therefore, with C# 14, the next strains of code will work completely.
Console.WriteLine(nameof(Checklist));
Console.WriteLine(nameof(Dictionary));
Person-defined compound task operators
C# 14 comes with help for compound task operators. This characteristic allows you to write code just like x += y as a substitute of getting to jot down x = x + y, as you do within the earlier variations of the language. You should use compound task operators in C# 14 to overload +=, -=, *=, /=, %=, &=, |=, ^=, <<=, and >>= operators.
Take into account the next code snippet that creates a ShoppingCart class by which the += operator is overloaded.
public class ShoppingCart
{
public int TotalQuantity { get; non-public set; } = 0;
public decimal TotalAmount { get; non-public set; } = 0m;
public void operator +=(int amount)
{
TotalQuantity += amount;
}
public void operator +=(decimal quantity)
{
TotalAmount += quantity;
}
}
The code snippet beneath exhibits how you should utilize the ShoppingCart class.
public class ShoppingCart
{
public int TotalQuantity { get; non-public set; } = 0;
public decimal TotalAmount { get; non-public set; } = 0m;
public void operator +=(int amount)
{
TotalQuantity += amount;
}
public void operator +=(decimal quantity)
{
TotalAmount += quantity;
}
}
Due to user-defined compound task operators, we get cleaner, easier, and extra readable code.
Set TargetFramework to .NET 10
Naturally, you need to have .NET 10 put in in your laptop to work with C# 14. If you wish to change your current initiatives to make use of C# 14, you will have to set the TargetFramework to .NET 10 as proven within the code snippet given beneath.
Exe
preview
net10.0
allow
allow
You possibly can study extra concerning the new options in C# 14 right here and right here.
The C# programming language has improved considerably since its preliminary launch as a part of Visible Studio .NET 2002. That was a time while you needed to write a whole lot of verbose code to create C# packages. The brand new options launched in C# 14 promise to spice up your productiveness and allow you to write cleaner, extra maintainable, and extra performant code. Whether or not you’re constructing an enterprise software, a cellular software, or an internet software, this new model of C# gives you with all you’ll want to create world-class up to date purposes.
