A
struct is a simple user-defined type, a lightweight alternative to a
class. They support access modifiers, constructors, indexers, methods, fields, nested types, operators, and properties. They may declare a default constructor (a constructor with no parameters), however you can also declare a custom constructor. Unlike classes, structures do not support compile-time initialization of instance fields, they cannot derive from anything other than
System.ValueType and they cannot be the base of another class. However, they may implement an Interface. They can be instantiated without using a
new operator.
Structures take up fewer resources in memory than classes, so when you have a small, frequently used class, give some thought to using a struct instead. Note though that performance can suffer when using structures in situations where reference types are expected due to boxing and unboxing.
struct members may not be declared
protected.
Declaration
A
struct is declared as follows:
Code:
[attributes] [modifiers] struct identifier [:interfaces]
{
body [;]
}
The
attributes is optional and is used to hold additional declarative information.
The
modifier is optional. The allowed modifiers are new, static, virtual, abstract, override, and a valid combination of the four access modifiers (public, protected, internal and private).
The keyword
struct must be followed by an identifier that names the struct.
The
interfaces list is optional. The list contains the interfaces implemented by the struct, all separated by commas.
The
body contains the member declarations.
Example 1 - A coordinate
Code:
public struct Coords
{
public int X, Y;
public Coords(int p1, int p2)
{
X = p1;
Y = p2;
}
// Override the ToString method so the value appears in text
public override string ToString()
{
return String.Format("({0},{1})", X, Y);
}
}
Which can be created in the following manner:
Code:
class Program
{
static void Main(string[] args)
{
Coords coord = new Coords(3,4);
Console.WriteLine(coord.ToString());
Console.ReadKey();
}
}
Please do rate my answer