16 November

C# 11 – Auto-default struct

Programming

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.


Let's talk

SEND THE EMAIL

I agree that my data in this form will be sent to [email protected] and will be read by human beings. We will answer you as soon as possible. If you sent this form by mistake or want to remove your data, you can let us know by sending an email to [email protected]. We will never send you any spam or share your data with third parties.

I agree that my data in this form will be sent to [email protected] and will be read by human beings. We will answer you as soon as possible. If you sent this form by mistake or want to remove your data, you can let us know by sending an email to [email protected]. We will never send you any spam or share your data with third parties.