FahmidasClassroom

Learn by easy steps

Feature Image

The structure or struct is used to define the advanced level user-defined data type in C#. It works like a class but the struct is value type not reference type. It is mainly used to define pre-defined data and it can contains many types of data. The struct keyword is used to declare structure in C#. The uses of the structure in C# have been in this tutorial by using various examples.

1000010832

Syntax:

struct <Name>
{
    <member1>,
    <member2>,
    <member3>,
    ...
    <memberN>,
}

Example-1: Simple use of structure

Create a C# file with the following code that shows the use of the structure data type in C#. Here, a simple structure of one data member has been defined that will be used to store the radius value of the circle. Next, the area of the circle will be calculated and printed.

using System;

namespace ConsoleApp1
{
    //Declare the structure
    struct Circle
    {
       public int radius;

    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the radius value of the circle:");
            //Convert input value into the integer
            int val = Convert.ToInt16(Console.ReadLine());
            //Create structure variable
            Circle rd = new Circle();
            //Set the value
            rd.radius = val;
            //Calculate the area
            double area = 3.14 * rd.radius * rd.radius;
            //Print the area value
            Console.WriteLine("Circle area is: " + area);
        }
    }
}

The following output will be appeared for the input value 5.

P1 5

Example-2: Use of structure to store record

Create a C# file with the following code that shows the use of the structure data type in C# to store multiple records of the students. Here, a structure of four data member has been defined that will be used to store the id, name, department, and cgpa of the students. Next, the three records of the students have been assigned by defining three structure variables. The student id will be taken from the user and if the matching record found then the corresponding student details will be printed.

using System;
using static System.Reflection.Metadata.BlobBuilder;

namespace ConsoleApp1
{
    //Declare the structure
    struct Students
    {
        public int id;
        public string name;
        public string dept;
        public double cgpa;
    };
    class Program
    {
        static void Main(string[] args)
        {
            //Create three object of the structure
            Students s1, s2, s3;

            //Assign values to the members of the first structure variable
            s1.id = 111785;
            s1.name = "Hasibur Rahman";
            s1.dept = "CSE";
            s1.cgpa = 3.89;

            //Assign values to the members of the second structure variable
            s2.id = 111790;
            s2.name = "Afroza Akter Lucky";
            s2.dept = "CSE";
            s2.cgpa = 3.64;

            //Assign values to the members of the third structure variable
            s3.id = 111795;
            s3.name = "Rajib Hossain";
            s3.dept = "BBA";
            s3.cgpa = 3.80;

            Console.WriteLine("Enter the student ID:");
            //Convert input value into the integer
            int ID = Convert.ToInt32(Console.ReadLine());

            //Check the id value and print message based on the id value
            if(ID == s1.id)
            {
                
                Console.WriteLine("ID: {0}, Name: {1}, Department: {2}, CGPA: {3}", s1.id, s1.name, s1.dept, s1.cgpa);
            }
            else if (ID == s2.id)
            {
                Console.WriteLine("ID: {0}, Name: {1}, Department: {2}, CGPA: {3}", s2.id, s2.name, s2.dept, s2.cgpa);
            }
            else if (ID == s1.id)
            {
                Console.WriteLine("ID: {0}, Name: {1}, Department: {2}, CGPA: {3}", s3.id, s3.name, s3.dept, s3.cgpa);
            }
            else
            {
                Console.WriteLine("No matching ID found.");
            }

        }
    }
}

The following output will appear for the input value, 111790 because this id is stored in the second structure variable.

P2 3
1000011159

The following output will appear for the input value, 111453 because this id does not match with any id value of the structure variable.

P3 3

Solve the following exercise to practice the use of the structure in C#.

Exercise:

Write the C# code to create a structure called Customers that contains three data members. These are id (int), name (string), and Purchased_amount (long int). Calculate the discount amount based on the purchased amount. The percentage of the discount amount will be calculated based on the following criteria.

A. 5% discount if the purchase amount is more than 1000 taka.

B. 10% discount if the purchase amount is more than 5000 taka.

C. 15% discount if the purchase amount is more than 10000 taka.