Nested Structs - C# Programming Exercise

This Exercise in C# involves creating a struct to store two pieces of data for a person: their name and their date of birth. The date of birth should be another struct consisting of day, month, and year. The goal of this Exercise is to practice using structs in C# and learn how to store and manage a person's information in an organized way.

In this Exercise, you will first create a struct called Person that will contain a field for the name and another for the date of birth, which will be defined by a nested struct called DateOfBirth. The DateOfBirth struct will have three fields: day, month, and year. Then, the program must create an array of persons, ask the user to input the data for two people, and then display that data.

This type of Exercise is a great way to practice using structs in C# to organize related data and interact with the user to gather and display information efficiently. Additionally, it reinforces understanding how to work with arrays of structs and data manipulation in C#.

 Category

Arrays, Structures and Strings

 Exercise

Nested Structs

 Objective

Write a C# Struct to store two data for a person:

name and date of birth.

The date of birth must be another struct consisting on day, month and year.

Finally, create an array of persons, ask the user for the data of two persons and display them.

 Write Your C# Exercise

using System;  // Importing the System namespace to access basic classes like Console

class Program
{
    // Define a struct for Date of Birth with three fields: Day, Month, and Year
    struct DateOfBirth
    {
        public int Day;    // Day of birth
        public int Month;  // Month of birth
        public int Year;   // Year of birth
    }

    // Define a struct for Person that includes a Name and a DateOfBirth
    struct Person
    {
        public string Name;          // Name of the person
        public DateOfBirth BirthDate; // Date of birth of the person
    }

    static void Main()
    {
        // Create an array of Persons to store the data for two people
        Person[] persons = new Person[2];

        // Ask the user to input data for two persons
        for (int i = 0; i < persons.Length; i++)
        {
            // Ask the user to enter the name of the person
            Console.Write($"Enter the name of person {i + 1}: ");
            persons[i].Name = Console.ReadLine();  // Store the name in the Name field of the struct

            // Ask the user to enter the birth day of the person
            Console.Write($"Enter the birth day of person {i + 1}: ");
            persons[i].BirthDate.Day = int.Parse(Console.ReadLine()); // Store the birth day in the Day field of the nested struct

            // Ask the user to enter the birth month of the person
            Console.Write($"Enter the birth month of person {i + 1}: ");
            persons[i].BirthDate.Month = int.Parse(Console.ReadLine()); // Store the birth month in the Month field of the nested struct

            // Ask the user to enter the birth year of the person
            Console.Write($"Enter the birth year of person {i + 1}: ");
            persons[i].BirthDate.Year = int.Parse(Console.ReadLine()); // Store the birth year in the Year field of the nested struct
        }

        // Display the data of both persons
        for (int i = 0; i < persons.Length; i++)
        {
            Console.WriteLine($"Person {i + 1}:");  // Print the person index (1 or 2)
            Console.WriteLine($"Name: {persons[i].Name}");  // Display the name of the person
            Console.WriteLine($"Birth Date: {persons[i].BirthDate.Day}/{persons[i].BirthDate.Month}/{persons[i].BirthDate.Year}");  // Display the birth date of the person
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Sort data

    This Exercise in C# involves creating a program that asks the user for 10 integer numbers (from -1000 to 1000), sorts them, and then displays them in ascending...

  •  Two dimensional array as buffer for screen

    This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" 80 letters (e.g., 'X') in random positions withi...

  •  Two dimensional array 2: circunference on screen

    This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" a circumference with a radius of 8 inside it. To...

  •  Computer programs

    Exercise in C# involves creating a program that can store up to 1000 records of computer programs. For each program, the following data should be stored: name,...

  •  Exercise tasks

    This exercise in C# consists of creating a program that can store up to 2000 "to-do tasks". Each task must store the following data: • Date (a set of 3 data: ...

  •  Household accounts

    This exercise in C# consists of creating a program that can store up to 10,000 costs and revenues, to create a small domestic accounting system. For eac...