Structure Data Type Aka Struct Data Type

After talking about Arrays in C#, this is the right time to talk about Structure data type in C#. There are a few limitations about Arrays, the array can only save a single type of data and you should define the number of elements going to be saved inside this array.

Structure-Data-Type-or-Struct-Data-Type

Why do We Need Structure Data Type

Imagine you want to save the data of an employee inside a variable. Because the data of an employee has different types of data like Name, Employee ID, Salary, and other things. There are no pre-defined data types that can hold all types of data. You would need a special data type for it. That special data type is structure.

Structure Data Type Or Struct Data Type

The structure is a value type of data type and does not support Inheritance. In case you want to learn about value type and reference type. Please read our Value Type and Reference Type Article on it. The Structure data type is a user-defined data type in which you can save the data of any type in a single variable. You will be the one who will decide what all types of data this variable will hold because you are defining this data type. Let talk about how to define a structure data type in coding.

Note – Do not worry If you do not understand anything about inheritance at this point. I will be writing an article on Inheritance once we start Object-Oriented Programming Concepts.

Structure Code Example

using System;

namespace Structure_Data_Type
{
    struct Employee
    {
        public double EmployeeID;
        public string Employee_Name;
        public double Salary;
    }

    class Program
    {
        static void Main(string[] args)
        {
            Employee Emp;
            Emp.EmployeeID = 12345;
            Emp.Employee_Name = "Angelina Jolie";
            Emp.Salary = 200000;

            Console.WriteLine("Employee ID -" + " " + Emp.EmployeeID);
            Console.WriteLine("Employee Name -" + " " + Emp.Employee_Name);
            Console.WriteLine("Employee Salary -" + " " + Emp.Salary);

            Console.ReadLine();
        }

        
    }
}

The output of the Program

Structure-Program-Output

Explanation

As you can see we have created a structure data type named Employee outside a class. After that, we have created an instance of an Employee data type named Emp inside the main method and we have initialized the variables inside Emp. Initialization means we have saved the data inside the variables. As you can see we have used the period (.) to access the components of Emp. This is how we save all types of data inside a single variable.

An array of Structure Data Type

We can also create an array of Structure data type. Let me give you a demonstration of the same example above.

using System;

namespace Structure_Data_Type
{
    struct Employee
    {
        public double EmployeeID;
        public string Employee_Name;
        public double Salary;
    }

    class Program
    {
        static void Main(string[] args)
        {
            Employee[] Emp = new Employee[10];
            Emp[0].EmployeeID = 12346;
            Emp[0].Employee_Name = "Will Smith";
            Emp[0].Salary = 400000;
            
            Emp[1].EmployeeID = 12347;
            Emp[1].Employee_Name = "Mark Smith";
            Emp[1].Salary = 20000;
           
            Emp[2].EmployeeID = 12348;
            Emp[2].Employee_Name = "Linda Smith";
            Emp[2].Salary = 2000;

            Console.WriteLine("Employee ID -" + " " + Emp[0].EmployeeID);
            Console.WriteLine("Employee Name -" + " " + Emp[0].Employee_Name);
            Console.WriteLine("Employee Salary -" + " " + Emp[0].Salary);

            Console.WriteLine("**********************************");

            Console.WriteLine("Employee ID -" + " " + Emp[1].EmployeeID);
            Console.WriteLine("Employee Name -" + " " + Emp[1].Employee_Name);
            Console.WriteLine("Employee Salary -" + " " + Emp[1].Salary);

            Console.WriteLine("**********************************");

            Console.WriteLine("Employee ID -" + " " + Emp[2].EmployeeID);
            Console.WriteLine("Employee Name -" + " " + Emp[2].Employee_Name);
            Console.WriteLine("Employee Salary -" + " " + Emp[2].Salary);


            Console.ReadLine();
        }

        
    }
}

Output

Array-Example-of-Structure-Program-Output

Explanation

As you can see in the first example, we created a structure data type named Employee and in the second example, we created the array of Employee Data Type. Everything else is the same as we do with an array. We have defined that 10 elements will be saved inside this array. We used indexes to access the elements of the array. We have saved the data of 3 employees in the array and access them with the help of the index.

I hope the concept of Structure is now clear in your mind, it is a very useful feature provided by Microsoft in C#. In case of any confusion feel free to comment. I will be glad to clear your confusion.

Have a Good One Guys.

Stay Safe Stay Healthy!