In this post, we will explore the upcoming features in C# 14 (.NET 10). You can try these features using .NET 10, currently in preview 3, and Visual Studio preview 17.14 or higher.
In this post, we will explore the following features.
- field Keyword
- Extension Members
- Null Conditional assignment
field Keyword
In previous versions of .NET, we were family with auto properties.
public int Foo { get; set; }
.NET traditionally always had the field-backed properties, which allowed developers to add more logic to the property accessors when required.
private int _foo;
public int Foo
{
get
{
// Do something
return _foo;
}
set
{
// Additional logic
_foo = value;
// Some more additional logic
}
}
But this approach had the extra step of creating the backing field.
With .NET 14, the field keyword allows developers to write a property accessor without explicitly declaring a backing field.
public int Foo
{
get
{
// Do something
return field;
}
set
{
// Additional logic
field = value;
// Some more additional logic
}
}
Now we have the best of both worlds, the properties are more concise without the need for creating an explicit backing field. The compiler replaces the field token with compiler-generated backing fields in the runtime.
[CompilerGenerated]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int <Foo>k__BackingField;
public int Foo
{
get
{
return <Foo>k__BackingField;
}
set
{
<Foo>k__BackingField = value;
}
}
Extension Members
Extension methods have been part of .NET for some time now. But what we lacked was a way to add an extension property. The current syntax has a sort of limitation in the ability to add extension properties and this could be the reason why the language developers decided to introduce a new syntax extension for members.
Previously, we could declare extension methods using the following format.
internal sealed class SpecialList<T>
{
public List<T> Items { get; private set; } = new List<T>();
public void Add(T item)
{
Items.Add(item);
}
}
internal static class ExtensionMembers
{
public static void InsertOne<T>(this SpecialList<T> source, int index, T item)
{
if (index < 0 || index > source.Items.Count)
{
throw new ArgumentOutOfRangeException(nameof(index), "Index is out of range.");
}
source.Items.Insert(index, item);
}
}

Join the conversation! Your thoughts help the community grow.