c-sharp
  1. c-sharp-program-to-store-information-of-students-using-structure

Program to Store Information of Students Using Structure - (C# Basic Programs)

Storing information about students is a common task in many applications. In this tutorial, we will discuss a program to store information about students using structures in C# programming language.

Syntax

The syntax for creating a structure to store student information in C# is as follows:

struct student {
   string name;
   int age;
   string address;
   string phone;
}

Example

using System;

namespace StudentInformation
{
    struct Student
    {
        public string name;
        public int age;
        public string address;
        public string phone;
    }

    class Program
    {
        static void Main(string[] args)
        {
            Student[] students = new Student[3];

            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine($"Enter details of student {i+1}:");
                Console.Write("Name: ");
                students[i].name = Console.ReadLine();
                Console.Write("Age: ");
                students[i].age = Convert.ToInt32(Console.ReadLine());
                Console.Write("Address: ");
                students[i].address = Console.ReadLine();
                Console.Write("Phone: ");
                students[i].phone = Console.ReadLine();
            }

            Console.WriteLine("\nStudent Information:");
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine($"Name: {students[i].name}\nAge: {students[i].age}\nAddress: {students[i].address}\nPhone: {students[i].phone}\n");
            }
        }
    }
}

Output:

Enter details of student 1:
Name: John
Age: 21
Address: 123 Main St.
Phone: 555-555-5555
Enter details of student 2:
Name: Sarah
Age: 20
Address: 456 Elm St.
Phone: 555-555-5556
Enter details of student 3:
Name: Bob
Age: 22
Address: 789 Oak St.
Phone: 555-555-5557

Student Information:
Name: John
Age: 21
Address: 123 Main St.
Phone: 555-555-5555

Name: Sarah
Age: 20
Address: 456 Elm St.
Phone: 555-555-5556

Name: Bob
Age: 22
Address: 789 Oak St.
Phone: 555-555-5557

Explanation

The program starts by defining a structure named Student, which consists of four fields: name, age, address, and phone.

The program then creates an array of Student structures with the size of 3. Next, the program uses a for loop to iterate through the array and collect details about each student from the user.

Finally, the program prints out the information of the students using another for loop.

Use

This program can be used as a basis for more complex applications that require storing information about students, such as a student management system. By using structures, it becomes easier to organize and manipulate the student data in a program.

Summary

In this tutorial, we have discussed a program to store information about students using structures in C# programming language. We have seen the syntax, example, explanation, and use of storing student data in C#. This program can be used as a starting point for creating more complex applications that require managing student data.

Published on: