Erik Movsesyan

Erik Movsesyan

  • NA
  • 62
  • 6.7k

Why does the struct parameterless constructor have to be public C#

Aug 7 2022 4:34 PM
struct plane {
    public int first;
    public int second = 5;

    public plane() { //has to be public
        //first = 99; error, all uninitialized instance fields must be initialized
    }

    plane(int h) { //can be private
        //first = 88; error, all uninitialized instance fields must be initialized
    }

}

 

I am trying to understand the whole concept of struct initialization:
I am sorry but I need to ask 2 questions because I think they are very dependent on eacg other:

Why does the parameterless constructor have to be public? I assume It is because the implicit constructor is public but this reason is just not enough to explain it.
why do we HAVE to initialize all the non-static uninitialized fields? I mean we could do it later manually.


Answers (3)