As well as class types, C# also has struct types. Struct types are declared and used like class types.
Suppose Fred is the following class type:
public class Fred { private int iX; private int iY; public Fred(int pX, int pY) { iX = pX; iY = pY; } ... }Then, in both Java and C#, an instance of Fred can be created by:
Fred tFredOne = new Fred(100, 200);
Fred tFredTwo = tFredOne;
A declaration of a struct type looks similar to that of a class type:
public struct Bert { private int iX; private int iY; public Bert(int pX, int pY) { iX = pX; iY = pY; } ... }
Bert tBertOne = new Bert(100, 200);
Bert tBertTwo = new Bert(0, 0);
tBertTwo = tBertOne;
Like any other local variable (e.g., a local variable of type int), a local variable that is of a struct type comes into being when the block starts and ceases to exist when the block is exited. This is quite different from an object (that is of some class, and is pointed to by some reference variable): the object is created by the use of new and ceases to exist when the garbage collector says so. In implementation terms, a local variable that is of a struct type is allocated on the stack whereas an object is allocated on the heap.