This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law. .NET 1.1+

# C# Structures

The twenty-second, and final, part of the C# Object-Oriented Programming tutorial reviews the use of structures. Structures provide similar functionality to classes, but when instantiated resultant variables are value types, rather than reference types.

The new structure requires properties to hold the X and Y co-ordinates. These will contain integer values. The syntax for creating a property is identical to that of creating a class property. However, if linking the property to a private field, as shown below, the field may not be initialised in its declaration.

To create the two properties, add the following code within the structure's code block:

```private int _x, _y;

public int X
{
get { return _x; }
set { _x = value; }
}

public int Y
{
get { return _y; }
set { _y = value; }
}```

We can now add a method to the structure that calculates the length of the vector using the Pythagorean Theorem and the values of the two co-ordinates. To define this method, add the following code:

```public double CalculateLength()
{
return Math.Sqrt(_x * _x + _y * _y);
}```

### Instantiating a Structure

There are two manners in which to instantiate a structure and create a variable. The first way is to use the "new" operator and create a variable using the same syntax as when creating an object of a class. The new keyword causes a constructor to be executed according to the parameters used. In the following sample, a new vector is created using the default constructor. This means that when first created, the _x and _y fields are assigned their default values of zero until changed.

```Vector v = new Vector();
Console.WriteLine("X: {0}\tY: {1}", v.X, v.Y);
Console.WriteLine("Length: {0}", v.CalculateLength());

v.X = 30;
v.Y = 40;
Console.WriteLine("X: {0}\tY: {1}", v.X, v.Y);
Console.WriteLine("Length: {0}", v.CalculateLength());

/* OUTPUT

X: 0    Y: 0
Length: 0
X: 30   Y: 40
Length: 50

*/```

### Instantiating a Structure Without the New Operator

A structure-type variable can be created without using the new keyword. In this case, all of the fields in the structure remain unassigned until their values have been set. In addition, the compiler sees the entire variable as unassigned unless every field has been given a value. If fields are not assigned a value the code will not compile. This is somewhat restrictive as it means that all of the fields in the structure must be public, limiting the good effects of encapsulation. However, for very simple structures this may be seen as acceptable.

To show this second method of instantiation, the structure must be updated to use public fields rather than properties. Modify the Vector code as follows:

```struct Vector
{
public int X, Y;

public double CalculateLength()
{
return Math.Sqrt(X * X + Y * Y);
}
}```

We can now instantiate the Vector structure without the new keyword as follows:

```Vector v;
v.X = 30;
v.Y = 40;
Console.WriteLine("X: {0}\tY: {1}", v.X, v.Y);
Console.WriteLine("Length: {0}", v.CalculateLength());

/* OUTPUT

X: 30   Y: 40
Length: 50

*/```

The final type of member that we will demonstrate in this article is a constructor. As with classes, multiple constructors can be added to a structure in order to initialise it in varying ways. The selection of the constructor to execute on instantiating a class is dependant upon the signature used with the "new" keyword.

To show the use of a new constructor, add the following declaration to the structure's code block. This constructor will allow the two co-ordinates to be set on instantiation:

```public Vector(int x, int y)
{
X = x;
Y = y;
}```

We can test the new constructor using the following code:

```Vector v = new Vector(30, 40);
Console.WriteLine("X: {0}\tY: {1}", v.X, v.Y);
Console.WriteLine("Length: {0}", v.CalculateLength());

/* OUTPUT

X: 30   Y: 40
Length: 50

*/```

NB: Unlike with class definitions, adding a new constructor does not remove the availability of the default constructor.

## Structures are Value Types

As described previously, the major difference between classes and structures is that structures are value types. Now that we have a structure, this can be demonstrated. In the following code, a Vector is instantiated and a copy is made. When the copy's fields are modified, there is no effect upon the original structure. This differs from the use of classes and their objects, as each variable would be a reference to the same data. If the data in one object were to change, the data in the other object would appear changed also.

```Vector v1 = new Vector(30, 40);
Vector v2 = v1;

v2.X = 10;
v2.Y = 15;

Console.WriteLine("X1: {0}\tY1: {1}", v1.X, v1.Y);
Console.WriteLine("X2: {0}\tY2: {1}", v2.X, v2.Y);

/* OUTPUT

X1: 30  Y1: 40
X2: 10  Y2: 15

*/```
5 May 2008