C# 10.0 is going to be out there soon along with the full release of .NET 6.0 and it has got some features this time that is going to make the code writing experience simpler and more aligned to the simplistic form of presentation. This article discusses the features available in C# 10.0 as of .NET 6.0 preview 7 and is due to be released alongside the full release of .NET 6.0 in November 2021.
Let’s go over some of the noteworthy features one by one,
Parameterless constructors in Structure types
While in C# 7.2 Microsoft introduced the ‘readonly’ attribute on structure types that are used to declare them as immutable and in C# 9.0 introduced a new ‘init’ keyword on properties that were also available on structure types. This time with C# 10.0, structure types have got another improvement in the form of the parameterless constructors.
You can declare a parameterless instance constructor in a structure type as the following example shows,
public struct FloorDimension {
public FloorDimension() {
Length = double.NaN;
Breadth = double.NaN;
UnitOfMeasure = "Undefined";
}
public FloorDimension(double length, double breadth, string unitOfMeasure) {
Length = length;
Breadth = breadth;
UnitOfMeasure = unitOfMeasure;
}
public double Length {
get;
init;
}
public double Breadth {
get;
init;
}
public string UnitOfMeasure {
get;
init;
};
public override string ToString() => $ "Length: {Length}, Breadth: {Breadth}, Unit: {UnitOfMeasure}";
}
public static void Main(string[] args) {
var d1 = new FloorDimension();
Console.WriteLine(d1);
// prints => Length: NaN, Breadth: NaN, Unit: Undefined
var d2 =
default (FloorDimension);
Console.WriteLine(d2);
// prints => Length: 0, Breadth: 0, Unit:
}
Instance field initialization in Structure types during declaration
From C# 10.0 onwards, the instance fields or properties of a structure type can be initialized where they are being declared. Let’s extend the above example to include initializers,
public struct FloorDimension {
public FloorDimension(double length, double breadth) {
Length = length;
Breadth = breadth;
}
public FloorDimension(double length, double breadth, string unitOfMeasure) {
Length = length;
Breadth = breadth;
UnitOfMeasure = unitOfMeasure;
}
public double Length {
get;
init;
}
public double Breadth {
get;
init;
}
public string UnitOfMeasure {
get;
init;
} = "meters";
public override string ToString() => $ "Length: {Length}, Breadth: {Breadth}, Unit: {UnitOfMeasure}";
}
public static void Main(string[] args) {
var d1 = new FloorDimension(4, 3);
Console.WriteLine(d1);
// prints => Length: 4, Breadth: 3, Unit: meters
}
In the case where a parameterless constructor is not declared explicitly, a structure type provides a parameterless constructor such that,
If a structure type has explicit instance constructors or has no field initializers, an implicit parameterless constructor produces the default value of a structure type, regardless of field initializers. So if we use the above-mentioned structure as an example with a parameterless constructor within the Main method, we have,
public static void Main(string[] args) {
var d1 = new FloorDimension();
Console.WriteLine(d1);
// prints => Length: 0, Breadth: 0, Unit:
}
If a structure type has no explicit instance constructors and has field initializers, the compiler synthesizes a public parameterless constructor that performs the specified field initializations. This can be illustrated with the below example,
public struct FloorDimension {
public double Length = double.NaN
public double Breadth = double.NaN
public string UnitOfMeasure = "meters";
public override string ToString() => $ "Length: {Length}, Breadth: {Breadth}, Unit: {UnitOfMeasure}";
}
public static void Main(string[] args) {
var d1 = new FloorDimension();
Console.WriteLine(d1);
// prints => Length: NaN, Breadth: NaN, Unit: meters
var d2 =
default (FloorDimension);
Console.WriteLine(d2);
// prints => Length: 0, Breadth: 0, Unit:
}
Join the conversation! Your thoughts help the community grow.