16 November
C# 11 – Auto-default struct
Programming
min. read

Let’s list all the features that C# 11 brings to the table. Let’s discuss them all one by one, with their use cases, and see how they can come in handy. At the bottom of each article, you can find a link to all 14 new C# features!
Auto-default struct
This is very small but the quality of life change. Consider the following.
public struct Vector3
{
public int X;
public int Y;
public int Z;
public Vector3(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
}
}
when we would try to comment out the Z field from this vector3 struct, we would get the following error in C# 10 Field 'Z' must be assigned upon exit
.
Now in C# 11, we can leave it as is
public struct Vector3
{
public int X;
public int Y;
public int Z;
public Vector3()
{
}
}
and the compiler will not longer complain. Behind the scenes, it will assign all of three values their default value of 0 in case of int. An empty string in case of string etc.
Further reading at dotnet GitHub page.
Author
About prog
Founded in 2016 in Warsaw, Poland. Prographers mission is to help the world put the sofware to work in new ways, through the delivery of custom tailored 3D and web applications to match the needs of the customers.
SIMILAR POSTS
C# 11 – Everything you need to know
Programming
min. read
C# 11 – Pattern matching on Spans
Programming
min. read
C# 11 – Files scoped types
Programming
min. read