16 November

C# 11 – Auto-default struct

min. read

Reading Time: < 1 minute
C# 11
C# 11

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

Tomasz Juszczak

CTO /

Technical Lead

Tomasz Juszczak

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.


Let's talk

SEND THE EMAIL