Structures
structures or structs are user-defined data types that can contain other variables as its fields and methods as its behavior. You can create your very own customized data type using structures. Let say you want a data that don’t just store a name as a string, but you also want to include his age and his monthly salary. To define a structure in a program, we use the following syntax.
struct StructName
{
member1;
member2;
member3;
...
member4;
}
We declare a structure using the struct keyword. Names of structures should used Pascal Casing to indicate that it is a structure, but that is just a convention followed in C# programming. The members can either be variables or methods. The program below shows an example of a structure.
using System;
namespace StructuresDemo
{
public struct Employee
{
public string name;
public int age;
public decimal salary;
}
public class Program
{
public static void Main()
{
Employee employee1;
Employee employee2;
employee1.name = "Jack";
employee1.age = 21;
employee1.salary = 1000;
employee2.name = "Mark";
employee2.age = 23;
employee2.salary = 800;
Console.WriteLine("Employee 1 Details");
Console.WriteLine("Name: {0}", employee1.name);
Console.WriteLine("Age: {0}", employee1.age);
Console.WriteLine("Salary: {0:C}", employee1.salary);
Console.WriteLine(); //Seperator
Console.WriteLine("Employee 2 Details");
Console.WriteLine("Name: {0}", employee2.name);
Console.WriteLine("Age: {0}", employee2.age);
Console.WriteLine("Salary: {0:C}", employee2.salary);
}
}
}
Example 1 – Using Structures
Employee 1 Details Name: Jack Age: 21 Salary: $1000.00 Employee 2 Datails Name: Mike Age: 23 Salary: $800.00
Let’s dissect the code to better understand the program. Lines 5-10 is the declaration of a structure. Note that we used the publickeyword. That keyword simply indicates that our Employee can be accessed or used anywhere in the program (or outside the program). public is one of the access specifiers and will be discussed in greater detail in later lessons. We used the struct keyword followed by the name of the structure. Structure names also follow the Pascal Casing convention. Inside the body of the structure, we defined three fields. Fields are like the properties of our Employee. For example, an employee has a name, an age and for he to continue his job, he is given a monthly salary. These members were also declared as public so they can be called outside the structure as demonstrated later.
Lines 16 and 17 declares two instances of Employee. The declaration of structure instances is similar to normal variables. You first indicate the structure type, and then the identifier.
Lines 19 to 25 assigned values to the respective fields of each employee. To access the fields outside the structure, they must be publicfirst (or internal as seen in a later lesson). We first write the name of the variable, followed by a dot (.) and then the name of the field. When the dot operators is used in this context, it is called the member access operator as it allows you to call the members of a particular structure (and class as we will see in the following lessons).
Lines 27 to 37 demonstrates how you can access the values stored on each field of a structure variable. When accessing or reading a field, we do the same thing. We indicate the variable name followed by a dot and then the name of the field to be read.
Structures are value types. That means that if we assign employee2 with a value of employee1, employee2 will copy all the values of the attributes of employee1 instead of getting its reference. A class is similar to a structure but it is a reference type. It will be discussed in a later lesson.
You can also add methods inside a structure. The example below modifies the program in Example 1.
using System;
namespace StructuresDemo2
{
public struct Employee
{
public string name;
public int age;
public decimal salary;
public void SayThanks()
{
Console.WriteLine("{0} thanked you!", name);
}
}
public class Program
{
public static void Main()
{
Employee employee1;
Employee employee2;
employee1.name = "Jack";
employee1.age = 21;
employee1.salary = 1000;
employee2.name = "Mark";
employee2.age = 23;
employee2.salary = 800;
Console.WriteLine("Employee 1 Details");
Console.WriteLine("Name: {0}", employee1.name);
Console.WriteLine("Age: {0}", employee1.age);
Console.WriteLine("Salary: {0:C}", employee1.salary);
employee1.SayThanks();
Console.WriteLine(); //Seperator
Console.WriteLine("Employee 2 Details");
Console.WriteLine("Name: {0}", employee2.name);
Console.WriteLine("Age: {0}", employee2.age);
Console.WriteLine("Salary: {0:C}", employee2.salary);
employee2.SayThanks();
}
}
}
Example 2 – Using Structures
Employee 1 Details Name: Jack Age: 21 Salary: $1000.00 Jack thanked you! Employee 2 Details Name: Mike Age: 23 Salary: $800.00 Mike thanked you!
Lines 11 to 14 define a new method inside a structure. This method prints a message and gets the value of the name field to show a unique message for each instance. To call the method, we used similar syntax except that instead of the field following the dot, we indicate the name of method to call followed by parentheses—and a set of arguments if they are required by a function.