Accessible as part of .NET 10, which was launched final November, C# 14 brings a plethora of latest options and enhancements that make it simpler to put in writing environment friendly, excessive performant code. Simply as we walked by way of the brand new options and enhancements in C# 13 and C# 12, on this article we’ll take a detailed have a look at a few of the finest new options in C# 14.
To work with the code examples supplied on this article, you must have Visible Studio 2026 or a later model put in in your system. If you happen to don’t have already got a duplicate, you’ll be able to obtain Visible Studio 2026 right here.
File-based apps
Help for file-based apps is maybe essentially the most putting new characteristic on this launch of the C# programming language. Till C# 14, we’ve needed to observe a multi-step course of to run a minimal .cs file. Not solely was this a multi-step course of, but it surely incurred important extra overhead since you needed to create an answer file and a undertaking file to run your utility. 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 extra information chances are you’ll not want later. Now not.
With C# 14, now you’ll be able to run a C# file instantly from the command line with no need a undertaking or resolution file.
Allow us to perceive this with a code instance. Contemplate 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($"Immediately's date is: {dateTime.ToString("d")}");
You’ll be able to 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
Word which you could create file-based apps that reference NuGet packages and SDKs utilizing preprocessor directives, with no need a undertaking 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 allows you to increase the capabilities of lessons with out the need of inheritance. You don’t want to create subclasses to make use of extension strategies, neither is it crucial to switch or recompile current class definitions. Along with bettering code readability, extension strategies enable you to add new strategies to your current sorts (i.e., lessons, structs, data, or interfaces). By the way, extension strategies had been first applied in C# 3.0.
There are quite a few extension strategies in .NET that can help you increase the querying capabilities of each System.Collections.IEnumerable and System.Collections.Generic.IEnumerable by utilizing the LINQ customary question operator. When you can make the most of extension strategies to increase a category or an interface in C#, you can not 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 technique 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 technique:
public static IEnumerable The place(
this IEnumerable supply,
Func predicate)
Word the usage of the this key phrase. Previous to C# 14, to implement an extension technique, you needed to create a static technique and cross the this reference as a parameter to the strategy. In C# 14, the code snippet above might be changed utilizing an extension block, with out the necessity of specifying the this parameter. That is proven within the code snippet given under.
extension(IEnumerable supply)
{
public IEnumerable
The place(Func predicate)
}
The flexibility to outline extension members has different benefits as effectively. Word that an extension member requires two sorts of data, i.e., the receiver to which the member ought 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 lets you outline a receiver on your extension member that doesn’t require any parameter, i.e., in the event you’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’ll be able to then outline a brand new extension block if the receiver adjustments. Furthermore, the static class through which you write your extension blocks or extension strategies (in the event you’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 under.
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 technique 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’ll be able to 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., Record, Dictionary). Now that nameof can take an unbound generic kind as an argument, you now not must outline dummy kind arguments (equivalent to Record) merely to acquire the sort title “Record.”
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(Record);
string typeNameDictionary = nameof(Dictionary);
With C# 14, unbound generics work instantly. You now not must specify the sort explicitly, as proven within the code snippet given under.
string typeNameList = nameof(Record);
string typeNameDictionary = nameof(Dictionary);
Therefore, with C# 14, the next strains of code will work completely.
Console.WriteLine(nameof(Record));
Console.WriteLine(nameof(Dictionary));
Person-defined compound project operators
C# 14 comes with assist for compound project operators. This characteristic lets you write code much like x += y as a substitute of getting to put in writing x = x + y, as you do within the earlier variations of the language. You should utilize compound project operators in C# 14 to overload +=, -=, *=, /=, %=, &=, |=, ^=, <<=, and >>= operators.
Contemplate the next code snippet that creates a ShoppingCart class through which the += operator is overloaded.
public class ShoppingCart
{
public int TotalQuantity { get; personal set; } = 0;
public decimal TotalAmount { get; personal set; } = 0m;
public void operator +=(int amount)
{
TotalQuantity += amount;
}
public void operator +=(decimal quantity)
{
TotalAmount += quantity;
}
}
The code snippet under exhibits how you should use the ShoppingCart class.
public class ShoppingCart
{
public int TotalQuantity { get; personal set; } = 0;
public decimal TotalAmount { get; personal set; } = 0m;
public void operator +=(int amount)
{
TotalQuantity += amount;
}
public void operator +=(decimal quantity)
{
TotalAmount += quantity;
}
}
Due to user-defined compound project operators, we get cleaner, less complicated, and extra readable code.
Set TargetFramework to .NET 10
Naturally, you could 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 have to to set the TargetFramework to .NET 10 as proven within the code snippet given under.
Exe
preview
net10.0
allow
allow
You’ll be able to be taught extra in regards to 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 once you needed to write lots of verbose code to create C# packages. The brand new options launched in C# 14 promise to spice up your productiveness and enable you to write cleaner, extra maintainable, and extra performant code. Whether or not you’re constructing an enterprise utility, a cellular utility, or an online utility, this new model of C# gives you with all you want to create world-class up to date purposes.
