Home » C# Basics » 04 - C# Programming - Part 2
4

Structures

Defines what structures are and how it is used in a C# program

Structures are used to store values of different data-types under one name. To understand this concept, let us discuss a simple example. Suppose we want to store the details of a student. The details may include the name, age, subject and so on. If we use simple variables, we will have to create a different variable for each detail. And if there are many students, things will become complicated.

Using a structure is one solution to this problem. A structure can store multiple values of different data-types.

The syntax for using a structure is as follows:

struct structure-name
{
 public variable1;
 public variable2;
 .
 .
 .
}

Here, struct is the keyword to declare the structure. The structure-name becomes the data-type. Inside the curly braces, required variables of the structure are declared with the keyword public. These variables are called the data members of the structure.

After declaring the structure, we declare a variable of the structure type. This is done as:

structure-name variable;

The following example illustrates the use of structures -

namespace AdvCsharp 
{ 
 struct Student 
 { 
   public string name; 
   public int age; 
   public double marks; 
  } 
class Program 
{ 
 static void Main(string[] args) 
 Student s;  
 Console.WriteLine("Enter the name of the student"); 
 s.name = Console.ReadLine(); 
 Console.WriteLine("Enter the age of the student"); 
 s.age = int.Parse(Console.ReadLine()); 
 Console.WriteLine("Enter the marks of the student"); 
 s.marks = double.Parse(Console.ReadLine());    
 Console.WriteLine("{0} is {1} years old and has {2}marks",s.name,s.age,s.marks ); 
} 
}
}

Like enumerators, structures are also declared outside the Main method. The name of our structure is `Student' and it has three data members - name, age and marks. Inside the Main method, we have declared a variable `s'. the data-type of `s' is `Student', the structure. This means that `s' is a structure variable.

To access the data members of the structure, we use the following syntax -

Structure-variable.data-member

So, s.name will access the data member `name', s.age will access the data member `age' and s.marks will access the data member `marks'. We can use this method to input values in the data members as well as display them.

Here is a sample output of the above program:

What if there are multiple students? Then we declare the structure variable as an array. In our example, it can be:

Student [] s = new Student[5];

This will create an array of structure. The structure-variable `s' can now store the details of 5 students. We can work with this array just like a normal array. For each student, the details can be accessed as:

s[i].name, s[i].age and s[i].marks where `i' is the array index.

The complete code and sample output is shown below -

namespace AdvCsharp 
{ 
struct Student 
{ 
public string name; 
public int age; 
public double marks; 
} 
class Program { static void Main(string[] args) 
{
 Student []s=new Student[3]; 
 int i;
 for (i = 0; i < 3; i++) 
{
Console.WriteLine("Enter the name of the student");  
s[i].name = Console.ReadLine();  
Console.WriteLine("Enter the age of the student");  
s[i].age = int.Parse(Console.ReadLine());  
Console.WriteLine("Enter the marks of the student");  
s[i].marks = double.Parse(Console.ReadLine()); }
for (i = 0; i < 3; i++) {
Console.WriteLine("{0} is {1} years old and has {2}marks", 
s[i].name, s[i].age, s[i].marks); 
} 
} 
} 
}

This chapter introduced us to some of the advanced concepts of C# programming. Next, we will take a look at one of the most important aspect of C# - the Object Oriented Programming.