Mixing Data Types using Structs - Page 1
       by kirupa  |  1 January 2007

Structs allow you to easily create objects that store mixed data types. A strongly-typed language such as C# provides many advantages, but let's say you want to use an Array to store three pieces of information: someone's first/last name and age. The names will be stored as a string, and the age will be stored as an integer. In a loosely-typed language such as ActionScript 2.0, you can get away with storing values of both string and integer (number) into your array, but in C# your can either store all strings or store all integers. You cannot mix and match.

You can deal with this constraint by creating a new data type that allows you to store both strings and integers. You can create such a new data type by using Structs!

On the Menu:

  1. Simple struct example.

  2. Important things to keep in mind.

  3. Using Properties

Simple Struct Example
Let's start off by looking at a simple example that shows how you use a struct and how to solve our mixed data type conundrum I mentioned earlier. Using the example from the intro, the following code creates a Person struct that allows you to specify the first and last name as a string and the age as an integer:

public struct Person
{
public string firstName;
public string lastName;
public int age;
}
class Program
{
static void Main(string[] args)
{
Person homer;
homer.firstName = "Homer";
homer.lastName = "Simpson";
homer.age = 36;
}
}

Let's look at this code in detail, even though it is pretty straightforward:

public struct Person
{
public string firstName;
public string lastName;
public int age;
}

I declare a struct called Person, and within the Person struct, declare three public variables of type string and int. In order to use our struct to store Person information, I use the following code from our Main method:

Person homer;
homer.firstName = "Homer";
homer.lastName = "Simpson";
homer.age = 36;

At the beginning of this article, I provided an example where you can use a struct to simulate storing mixed data types into an array. The following code does just that:

static void Main(string[] args)
{
Person homer = new Person();
homer.FirstName = "Homer";
homer.LastName = "Simpson";
homer.Age = 36;
 
Person[] foo = new Person[10];
foo[0] = homer;
 
Console.WriteLine("Person's age is {0}", foo[0].Age);
}

Notice that my List takes values of type Person, and that is great because our Person struct stores values of types string and int. We have achieved our goal of storing mixed data types in C#!

Important Things to Keep in Mind
The important things to keep in mind when using structs are:

  1. You are not required to define a a constructor. Notice that my Person struct does not contain a constructor, but you can use one only if your constructor takes in parameters. You cannot have a default constructor.
  2. When instantiating an object of type struct, you do not need to qualify your instantiation with the new operator. For this simple example, I declare a Person object called homer as: Person homer; As you will see, when using Properties in the next page, you will need to use the new operator though.
  3. Structs can neither be inherited, nor can they be set as the base for another class. But, you can implement interfaces with structs though.
  4. On the surface, structs and classes seem very similar. One major difference is in performance. Because of the way structs are implemented (on the stack), they are faster in many cases than classes which are referenced from the heap.

There are other details that I will not cover in this article, because explaining them would deviate too much from other interesting things I want to explain, but you can read about them in the MSDN documentation: http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx

Onwards to the next page!

1 | 2




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.