Fundamentals of Structures

Introduction

A structure is an enhanced version of the primitive data types we have used in previous lessons. Like a class, a structure is created from one variable of a primitive type or by combining various variables of primitive types.

To create a structure, you use the same formula as for a class but with the struct keyword. Here is an example of a structure:

```struct Integer
{
}```

Like a class, a structure can have fields. They are listed in the body of the structure. Here is an example:

```struct Integer
{
private int val;
}```

A structure can also have properties. Here is an example:

```struct Integer
{
private int val;

public int Value
{
get { return val; }
set { val = value; }
}
}```

A structure can also have methods. Here is an example:

```struct Integer
{
private int val;

public int Value
{
get { return val; }
set { val = value; }
}

{
}
}```

Structure Declaration

Like any other data type, to use a structure, you can first declare a variable from it. To allocation memory for a variable declared from a structure, use the new operator as done for a class. Here is an example:

```using System;

struct Integer
{
private int val;

public int Value
{
get { return val; }
set { val = value; }
}

{
}
}

class Program
{
static int Main()
{
Integer Natural = new Integer();
return 0;
}
}```

As done for variables of the other types and as seen for classes, to declare a variable for a structure, you can use the var keyword. After declaring the variable, you can use the object the same way you would a class. You can access its members (fields, properties, and methods) using the period operator. Here is an example:

```using System;

struct Integer
{
private int val;

public int Value
{
get { return val; }
set { val = value; }
}

{
}
}

class Program
{
static int Main()
{
var Natural = new Integer();

Console.Write("Enter a natural number: ");
// Accessing a property of the structure
Natural.Value =
// Calling a method of the structure

Console.WriteLine("The value you entered was: {0}", Natural.Value);
return 0;
}
}```

Here is an example of running the program:

```Enter a natural number: 248
The value you entered was: 248
Press any key to continue . . .```

Although there are many similarities in the behaviors of classes and structures, you should use a structure when the object you are creating is meant to represent relatively small values. Like primitive data types and unlike a class, a structure is a value type.

Techniques of Using Structures

A Structure as a Property

Once a structure exists, you can use it like a data type. For example, you can create a property that is a structure type. The rules are the same we reviewed for creating a property of a class. After creating the property, you can use it as you see fit. Here is an example:

```using System;

public struct Real
{
private double val;

public double Value
{
get { return val; }
set { val = value; }
}

{
}
}

public struct Rectangle
{
Real len;
Real hgt;

public Real Length
{
get { return len; }
set { len = value; }
}

public Real Height
{
get { return hgt; }
set { hgt = value; }
}

public void CreateRectangle()
{
Real rat = new Real();

Console.WriteLine("Enter the dimensions of the rectangle");
Console.Write("Enter the length: ");
Console.Write("Enter the height: ");
}
}

public class Program
{
static int Main()
{
var Rect = new Rectangle();

Rect.CreateRectangle();
Console.WriteLine();

Console.WriteLine("Rectangle Characteristics");
Console.WriteLine("Length:    {0}", Rect.Length.Value);
Console.WriteLine("Height:    {0}", Rect.Height.Value);
Console.WriteLine("Perimeter: {0}",
(Rect.Length.Value + Rect.Height.Value) * 2);
Console.WriteLine("Area:      {0}",
Rect.Length.Value * Rect.Height.Value);

return 0;
}
}```

Here is an example of running the program:

```Enter the dimensions of the rectangle
Enter the length: 44.84
Enter the height: 26.75

Rectangle Characteristics
Length:    44.84
Height:    26.75
Perimeter: 143.18
Area:      1199.47
Press any key to continue . . .```

Returning a Structure From a Method

Like regular data type or a class, a structure can serve as the return type of a method.  The rules are more related to those of a class. When creating the method, type the name of the structure on the left side of the name of the method. In the body of the method, implement the desired behavior. Before exiting the method, make sure you return a valid value that is of the type of the structure. When a method returns a value of the type of a structure, you can assign the method to a variable of the type of the structure.

Here is an example of implementing a method that returns a structure type, including calling the method and using its value:

```using System;

public struct Real
{
private double val;

public double Value
{
get { return val; }
set { val = value; }
}

{
}
}

public struct Rectangle
{
Real len;
Real hgt;

public Real Length
{
get { return len; }
set { len = value; }
}

public Real Height
{
get { return hgt; }
set { hgt = value; }
}

public void CreateRectangle()
{
Real rat = new Real();

Console.WriteLine("Enter the dimensions of the rectangle");
len = GetLength();
Console.Write("Enter the height: ");
}

public Real GetLength()
{
Real rat = new Real();

Console.Write("Enter the length: ");
return rat;
}
}

public class Program
{
static int Main()
{
var Rect = new Rectangle();

Rect.CreateRectangle();
Console.WriteLine();

Console.WriteLine("Rectangle Characteristics");
Console.WriteLine("Length:    {0}", Rect.Length.Value);
Console.WriteLine("Height:    {0}", Rect.Height.Value);
Console.WriteLine("Perimeter: {0}",
(Rect.Length.Value + Rect.Height.Value) * 2);
Console.WriteLine("Area:      {0}",
Rect.Length.Value * Rect.Height.Value);

return 0;
}
}```

Here is an example of running the application:

```Enter the dimensions of the rectangle
Enter the length: 36.04
Enter the height: 22.86

Rectangle Characteristics
Length:    36.04
Height:    22.86
Perimeter: 117.8
Area:      823.8744
Press any key to continue . . .```

Passing a Structure as Argument

Like a data type, a structure can be passed as argument to a method. The argument is primarily passed as done for a class. After passing the argument, in the body of the method, you can access the public members of the structure, using the period operator. Here is an example:

```using System;

public struct Real
{
private double val;

public double Value
{
get { return val; }
set { val = value; }
}

{
}
}

public struct Rectangle
{
Real len;
Real hgt;

public Real Length
{
get { return len; }
set { len = value; }
}

public Real Height
{
get { return hgt; }
set { hgt = value; }
}

public void CreateRectangle()
{
Real rat = new Real();

Console.WriteLine("Enter the dimensions of the rectangle");
len = GetLength();
Console.Write("Enter the height: ");
}

public Real GetLength()
{
Real rat = new Real();

Console.Write("Enter the length: ");
return rat;
}
}

public class Program
{
public static void ShowCharacteristics(Rectangle rect)
{
Console.WriteLine("Rectangle Characteristics");
Console.WriteLine("Length:    {0}", rect.Length.Value);
Console.WriteLine("Height:    {0}", rect.Height.Value);
Console.WriteLine("Perimeter: {0}",
(rect.Length.Value + rect.Height.Value) * 2);
Console.WriteLine("Area:      {0}",
rect.Length.Value * rect.Height.Value);
}
static int Main()
{
var r = new Rectangle();
Rect.CreateRectangle();

Console.WriteLine();
ShowCharacteristics(r);

return 0;
}
}```

Here is an example of running the program:

```Enter the dimensions of the rectangle
Enter the length: 114.55
Enter the height: 82.72

Rectangle Characteristics
Length:    114.55
Height:    82.72
Perimeter: 394.54
Area:      9475.576
Press any key to continue . . .```

When you pass a structure to a method as we did above, it referred to as passing by value. A copy of the value of the structure is passed to the method. If the method modifies the argument, the original variable would stay intact. If you want the method to modify the value of the structure, you can pass the argument by reference. You can do this using the (rules of the) ref and the out keywords.

Here is an example of passing a structure by reference using the ref keyword:

```using System;

public struct Real
{
private double val;

public double Value
{
get { return val; }
set { val = value; }
}

{
}
}

public struct Rectangle
{
Real len;
Real hgt;

public Real Length
{
get { return len; }
set { len = value; }
}

public Real Height
{
get { return hgt; }
set { hgt = value; }
}

public void CreateRectangle()
{
Console.WriteLine("Enter the dimensions of the rectangle");
len = GetLength();
GetHeight(ref hgt);
}

public Real GetLength()
{
Real rat = new Real();

Console.Write("Enter the length: ");
return rat;
}

public void GetHeight(ref Real rl)
{
Real rat = new Real();

Console.Write("Enter the height: ");
}
}

public class Program
{
static int Main()
{
Rectangle rect = new Rectangle();

rect.CreateRectangle();
Console.WriteLine();

Console.WriteLine("Rectangle Characteristics");
Console.WriteLine("Length:    {0}", rect.Length.Value);
Console.WriteLine("Height:    {0}", rect.Height.Value);
Console.WriteLine("Perimeter: {0}",
(rect.Length.Value + rect.Height.Value) * 2);
Console.WriteLine("Area:      {0}",
rect.Length.Value * rect.Height.Value);

return 0;
}
}```

Here is an example of running the program:

```Enter the dimensions of the rectangle
Enter the length: 75.82
Enter the height: 55.64

Rectangle Characteristics
Length:    75.82
Height:    55.64
Perimeter: 262.92
Area:      4218.6248
Press any key to continue . . .```