Structure types are well suited for modeling mathemetical,geometrical, and other "atomic" entities in your application. A structure is a used-defined type. In C#, structures are defined using the "struct" keyword.
struct Point
{
// Fields of the structure
public int X;
public int Y;
// Add 1 to the (x,y) position
public void Increment()
{
X++; Y++;
}
// Display
public void Display()
{
Console.WriteLine(X + " " + Y);
}
}
// Create an initial Point
Point myPoint;
myPoint.X = 349;
myPoint.Y = 76;
myPoint.Increment();
myPoint.Display();
A structure can't inherit from other class or structure types and can't be the base of a class. Structures can implement interfaces.
Creating Stucture Variables
Simply create a Point variable and assign each piece of public field data before invoking its data members. If you don't assign each piece of public field data (X and Y in this case) before using the structure, you will receive a compiler error.
// Error! Did not assign Y value.
Point p1;
p1.X = 10;
p1.Display();
// OK! Both fields assigned before use.
Point p2;
p2.X = 10;
p2.Y = 10;
p2.Display();
As an alternative, you can create structure variables using the C# new keyword, which will invoke the structures default constructor. By definition, a default constructor does not take any arguments. The benefit of invoking the default constructor of a structure is that each piece of field data is automatically set to its default value.
// Set all fields to default values, using the default constructor.
Point p1 = new Point();
p1.Display(); // X=0,Y=0
Structure Constructors (Updated 10.0)
It's also possible to design a structure with a custom constructor. This allows you to specify the values of field data upon variable creation,rather than having to set each data member field by field.
struct Point
{
// Fields of structure
public int X;
public int Y;
// Custom Constructor
public Point(int xPos, int yPos)
{
X = xPos;
Y = yPos;
}
// ....
}
Point p1 = new Point(50, 100);
Regardless of which constructor you choose to add, prior C# 10, you could not declare a parameterless constructor on a structure, as it was provided in the implementation of structure types. Now, this is possible, as long as all value types are assigned a value before the code in the constructor end.
struct Point
{
// omitted for brevity
// Parameterless constructor
public Point()
{
X = 0;
Y = 0;
}
// A custom constructor
public Point(int xPos, int yPos)
{
X = xPos;
Y = yPos;
}
//...
}
Using the Field Initializors (New 10.0)
New on C# 10, structure fields can be initialized when declared.
struct Point
{
public int X = 5;
public int Y = 10;
// omitted for brevity
// the parameterless constructor no longer needs to initialize X and Y fields.
public Point() { }
}
Using Read-Only Structs (New 7.2)
Structs can also be marked as read-only if there is a need for them to be immutable. Immutable objects must be set-up at construction and beause they cannot be changed, can be more performant. When declaring a struct as readonly, all the properties must also be readonly.
readonly struct ReadOnlyPoint
{
public int X { get; }
public int Y { get; }
public void Display()
{
Console.WriteLine(X + " " + Y);
}
public ReadOnlyPoint(int xPos, int yPos)
{
X = xPos;
Y = yPos;
}
}
The Increment method has been removed since the variables are read-only.
Using Read-Only members (8.0)
New in C# 8.0, you can declare individual fields of a struct as read-only. The readonly modifier can be applied to methods, properties and property accessors.
struct PointWithReadOnly
{
public int X;
public int Y;
public readonly string Name;
public readonly void Display()
{
Console.WriteLine(X+" "+Y+" "+Name);
}
public PointWithReadOnly(int xPos,int yPos,string name)
{
X = xPos;
Y = yPos;
Name = name;
}
}