Erik Movsesyan

Erik Movsesyan

  • NA
  • 62
  • 5.9k

Does the constructor of a struct initialize the fields to defaults?

Aug 7 2022 11:20 AM


class aa{

    public int j=8;
    int f;

    public aa() {
        f=99;
    }

}

struct  bb {

    public int v = 7;
    int l;

    public bb() {
        l = 88;
    }

}

 

I am kind of confused as I am learning about the structs.

let's say if I create an instance of class aa, is the field j initialized to 8 which is NOT done by the constructor ? and field f is first defaulted to 0, again NOT by the constructor and then initialized to 99 in a constructor

And let's say if I create an instance of struct bb, is the v initialized to 7 which is NOT done by the constructor? And field l is FIRST defaulted to 0 NOT but the constructor? And then initialized to 88 by the constructor


Answers (1)