A struct is similar to a class.
A struct is a value type, whereas a class is a reference type.
A struct does not support inheritance.
A struct is implicitly deriving from object
, or more precisely, System.ValueType
.
A struct can have all the members a class can, except the following:
A parameterless constructor that you can't override implicitly exists. It performs a bitwise-zeroing of its fields.
When you define a struct constructor, you must explicitly assign every field.
You can't have field initializers in a struct.
Here is an example of declaring and calling struct constructors:
public struct Point { int x, y; public Point (int x, int y) { this.x = x; this.y = y; } } 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