A struct (short for “structure”) is very similar to a class and it can contain fields, properties, and methods.
However, structs are value types and are created on the stack.
Note:
You cannot inherit from a struct.
A struct can implement an interface.
structs have limited constructor and destructor semantics.
Why Structs ?
Typically, structs are used to aggregate a relatively small amount of logically related fields.
Structures are declared in the same way as classes.
This example shows what might be the start of a struct for imaginary numbers:
- struct ImaginaryNumber{
- double real;
- public double Real{
- get { return real; }
- set { real = value; }
- }
- double i;
- public double I{
- get { return i; }
- set { i = value; }
- }
- }
..