Structure:
A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. The declaration of a struct takes the following form:
Syntax:
[attributes] [modifiers] struct identifier [:interfaces] body [;]
where:
attributes (Optional)
Additional declarative information. For more information on attributes and attribute classes, see 17. Attributes.
modifiers (Optional)
The allowed modifiers are new and the four access modifiers.
identifier
The struct name.
interfaces (Optional)
A list that contains the interfaces implemented by the struct, all separated by commas.
body
The struct body that contains member declarations.
Example:
using System;
public struct Point
{
public int x, y;
public Point(int p1, int p2)
{
x = p1;
y = p2;
}
}
class MainClass
{
public static void Main()
{
Point myPoint = new Point();
Point yourPoint = new Point(10,10);
Console.Write("My Point: ");
Console.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y);
Console.Write("Your Point: ");
Console.WriteLine("x = {0}, y = {1}", yourPoint.x, yourPoint.y);
}
}
Output:
My Point: x = 0, y = 0
Your Point: x = 10, y = 10