Struct is like a class but is a value type and it does not support inheritance. Useful when value-type semantics are desirable. You do not need to define a parameterless constructor but if you do define than you must explictly assign every field.

Struct cannot have:

  • Parameterless constructor
  • Field initializers
  • A finalizer
  • Virtual or protected members
public struct Point
	public int X, Y;
	public Point (int x, int y) { X = x; Y = y; }
	// The parameterless constructor is implicit.

static void Main()
	Point p1 = new Point ();       // p1.x and p1.y will be 0
	Point p2 = new Point (1, 1);   // p1.x and p1.y will be 1

public struct Point
	int x = 1;	// Illegal: cannot initialize field
	int y;
	public Point() { }	// Illegal: cannot have parameterless constructor	
	public Point (int x) { this.x = x; }	// Illegal: must assign field y

static void Main() { }

Leave a Reply

Your email address will not be published. Required fields are marked *