Let's say we have the following code:
struct Point {
public Int32 m_x, m_y;
}
class Rectangle {
Point p;
}
//Program.cs
static void Main(string[] args) {
Point p;
int i = p.m_x // does't compile, unassigned field m_x
}
so we need to initialize the struct:
static void Main(string[] args) {
Point p;
p.m_x = 0;
int i = p.m_x; // compile OK now
}
or we can all struct's default parameterless constructor to initialize its fields:
static void Main(string[] args) {
Point p = new P();
int i = p.m_x //OK
}
so it looks like we need to initialize structs, but if the struct is a field in a in a class then it doesn't need to be initialized:
static void Main(string[] args) {
Rectangle r = new Rectangle();
int i = r.p.m_x; // comile OK
}
it still compile, even though I didn't initialize structs. And I checked the IL code and found Rectangle
's constructor doesn't call the struct's parameterless constructor neither.