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

前端 未结 4 1942
天命终不由人
天命终不由人 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:07

    It's for performance. When you new up a struct with the default constructor, all it does is allocate however many bytes on the stack and initialises them to 0.

提交回复
热议问题