0

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.

  • 5
    "*In C#, you must initialize a declared variable before it can be used. Because a structure-type variable can't be null (unless it's a variable of a nullable value type), you must instantiate an instance of the corresponding type*" - Fields that are of a value type, do not need to be initialized, they however will be `default` – TheGeneral Feb 04 '21 at 01:58
  • @JohnG sorry I was coping my code, forget to comment out –  Feb 04 '21 at 02:01
  • The compiler may elide the actual default initializer, as the default value (using `localsinit` rather than an actual constructor) is the same anyway. Furthermore, it's not your (calling code's) business how `Rectangle` initializes, just that it does so, and therefore `new Rectangle()` is guaranteed to have an initialized field. Aside: `Rectangle` is a struct, not a class, not that it matters. – Charlieface Feb 04 '21 at 02:12
  • @Charlieface `Rectangle` is a class in my code –  Feb 04 '21 at 02:21

1 Answers1

1

A local variable need to be initialised:

Instantiation of a structure type

In C#, you must initialize a declared variable before it can be used. Because a structure-type variable can't be null (unless it's a variable of a nullable value type), you must instantiate an instance of the corresponding type.

In regards to fields, let's look at the C# ECMA 334 specification

15.5.5 Field initialization

The initial value of a field, whether it be a static field or an instance field, is the default value

10.3 Default values

...

The default value of a variable depends on the type of the variable and is determined as follows:

  • For a variable of a value-type, the default value is the same as the value computed by the value-type’s default constructor.

9.3.3 Default constructors

All value types implicitly declare a public parameterless instance constructor called the default constructor. The default constructor returns a zero-initialized instance known as the default value for the value type:

  • For all simple-types, the default value is the value produced by a bit pattern of all zeros:

    • For sbyte, byte, short, ushort, int, uint, long, and ulong, the default value is 0.
    • For char, the default value is '\x0000'.
    • For float, the default value is 0.0f.
    • For double, the default value is 0.0d.
    • For decimal, the default value is 0.0m.
    • For bool, the default value is false.
  • For an enum-type E, the default value is 0, converted to the type E. 9 Types

  • For a struct-type, the default value is the value produced by setting all value type fields to their default value and all reference type fields to null.

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141