w3resource

C#: Create a nested structure and store data in an array

C# Sharp STRUCTURE: Exercise-3 with Solution

Write a C# Sharp program to create a nested struct that stores two data for an employee.

Sample Solution:-

C# Sharp Code:

using System;

class strucExer3
{
    // Definition of the employee structure
    struct employee
    {
        public string eName; // Member to store employee name
        public dtObirth Date; // Member to store date of birth (nested structure)
    }

    // Definition of the dtObirth structure (nested within the employee structure)
    struct dtObirth
    {
        public int Day; // Member to store day
        public int Month; // Member to store month
        public int Year; // Member to store year
    }

    static void Main(string[] args)
    {
        // Declaration of variables to store day, month, year, and total employees
        int dd = 0, mm = 0, yy = 0;
        int total = 2;

        // Displaying a message indicating the process to create a nested structure and store data in an array
        Console.Write("\n\nCreate a nested structure and store data in an array :\n");
        Console.Write("-------------------------------------------------------\n");

        // Array to store employees
        employee[] emp = new employee[total];

        // Loop to input data for multiple employees
        for (int i = 0; i < total; i++)
        {
            // Input employee name
            Console.Write("Name of the employee : ");
            string nm = Console.ReadLine();
            emp[i].eName = nm;

            // Input day of birth
            Console.Write("Input day of the birth : ");
            dd = Convert.ToInt32(Console.ReadLine());
            emp[i].Date.Day = dd;

            // Input month of birth
            Console.Write("Input month of the birth : ");
            mm = Convert.ToInt32(Console.ReadLine());
            emp[i].Date.Month = mm;

            // Input year of birth
            Console.Write("Input year for the birth : ");
            yy = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine();
            emp[i].Date.Year = yy;
        }
    }
}

Sample Output:

Create a nested structure and store data in an array :                                                           
-------------------------------------------------------                                                       
Name of the employee : H.Rana                                                                                 
Input day of the birth : 04                                                                                   
Input month of the birth : 05                                                                                 
Input year for the birth : 1787

                                                                                
Name of the employee : D.Das                                                                                  
Input day of the birth : 08                                                                                   
Input month of the birth : 04                                                                                 
Input year for the birth : 1784  

Flowchart:

Flowchart: Create a nested structure and store data in an array.

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to declare a simple structure and use of static fields inside a struct.
Next: Write a program in C# Sharp to create a structure and assign the value and call it.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.