Why can't I initialize my fields in my structs?

前端 未结 4 1936
天命终不由人
天命终不由人 2020-12-21 02:40

I am surprised that I can\'t initialize my fields in structs, why is it like that? Like:

struct MyStruct
{
    private int a = 90;
}

but it

4条回答
  •  执念已碎
    2020-12-21 03:19

    That's because your assignment is actually transformed by the compiler to be done in the default constructor. But C# structs don't have default constructors, as you can see in the link posted by Kent Boogaart.

提交回复
热议问题