Wednesday 18 April 2012

Declaring a Constructor for a Struct


The syntax we use to declare a constructor is the same for a struct as it is for a class. For example, the following is a struct called Point that has a constructor:







struct Point
{
public Point(int x, int y) { ... }
...
}

Struct Constructor Restrictions
Although the syntax for struct and class constructors is the same, there are some additional restrictions that apply to struct constructors:
The compiler always creates a default struct constructor.
You cannot declare a default constructor in a struct.
You cannot declare a protected constructor in a struct.
You must initialize all fields.
The Compiler Always Creates a Default Struct Constructor
The compiler always generates a default constructor, regardless of whether we declare constructors ourself. (This is unlike the situation with classes, in which the compiler-generated default constructor is only generated if you do not declare any constructors yourself.) The compiler generated struct constructor initializes all fields to zero, false, or null.

struct SPoint
{
  public SPoint(int x, int y) { ... }
  ...

  static void Main( )
  {
    // Okay
    SPoint p = new SPoint( );
  }
}

class CPoint
  {
  public CPoint(int x, int y) { ... }
  ...

  static void Main( )
  {
    // Compile-time error
    CPoint p = new CPoint( );
  }
}

This means that a struct value created with

SPoint p = new SPoint( );

creates a new struct value on the stack (using new to create a struct does not acquire memory from the heap) and initializes the fields to zero. There is no way to change this behavior.

However, a struct value created with

SPoint p;

still creates a struct value on the stack but does not initialize any of the fields (so any field must be definitely assigned before it can be referenced). Following is an example:

struct SPoint
{
  public int x, y;
  ...

  static void Main( )
  {
    SPoint p1;
    Console.WriteLine(p1.x); // Compile-time error
    SPoint p2;
    p2.x = 0;
    Console.WriteLine(p2.x); // Okay
  }
}   
We Cannot Declare a Default Constructor in a Struct
The reason for this restriction is that the compiler always creates a default constructor in a struct (as just described), so you would end up with a duplicate definition.

class CPoint
{
  // Okay because CPoint is a class
  public CPoint( ) { ... }
  ...
}

struct SPoint
{
  // Compile-time error because SPoint is a struct
  public SPoint( ) { ... }
  ...
}

We can declare a struct constructor as long as it expects at least one argument. If we declare a struct constructor, it will not automatically initialize any field to a default value (unlike the compiler-generated struct default constructor which will).

struct SPoint
{
public SPoint(int x, int y) { ... }
...
}
You Cannot Declare a Protected Constructor in a Struct
The reason for this restriction is that we can never derive other classes or structs from a struct, and so protected access would not make sense, as shown in the following example:

class CPoint
{
  // Okay
  protected CPoint(int x, int y) { ... }
}

struct SPoint
{
  // Compile-time error
  protected SPoint(int x, int y) { ... }
}
You Must Initialize All Fields
If we declare a class constructor that fails to initialize a field, the compiler will ensure that the field nevertheless retains its default zero initialization. The following code provides an example:

class CPoint
{
  private int x, y;
  public CPoint(int x, int y) { /*nothing*/ }
  // Okay. Compiler ensures that x and y are initialized to
  // zero.
}

However, if we declare a struct constructor that fails to initialize a field, the compiler will generate a compile-time error:

struct SPoint1 // Okay: initialized when declared
{
  private int x,y;
  public SPoint1(int a, int b) { }
}

struct SPoint2 // Okay: initialized in constructor
{
  private int x, y;
  public SPoint2(int x, int y)
  {
    this.x = x;
    this.y = y;
  }

0 comments: