Student Management System in C# (Console Application)

Managing student data is an integral part of educational institutions. In this post, we'll develop a simple Student Management System using a C# console application that will handle basic CRUD (Create, Read, Update, and Delete) operations. Let's get started!

Setting up the Project 

1. Open Visual Studio

2. Create a new Console App project

3. Designing the Student Model 

Before we dive into the operations, let's design our Student class.

4. Building the Management System

public class StudentManagementSystem
{
    private List<Student> students = new List<Student>();

    public void AddStudent(int rollNo, string name, double marks)
    {
        students.Add(new Student { RollNo = rollNo, Name = name, Marks = marks });
    }

    public void DisplayStudents()
    {
        foreach (var student in students)
        {
            Console.WriteLine($"Roll No: {student.RollNo}, Name: {student.Name}, Marks: {student.Marks}");
        }
    }

    public void UpdateStudent(int rollNo, string newName, double newMarks)
    {
        var student = students.FirstOrDefault(s => s.RollNo == rollNo);
        if (student != null)
        {
            student.Name = newName;
            student.Marks = newMarks;
        }
    }

    public void DeleteStudent(int rollNo)
    {
        var studentToRemove = students.FirstOrDefault(s => s.RollNo == rollNo);
        if (studentToRemove != null)
        {
            students.Remove(studentToRemove);
        }
    }
}

5. Implementing the Main Program

class Program
{
    static void Main(string[] args)
    {
        StudentManagementSystem sms = new StudentManagementSystem();

        int choice;

        do
        {
            Console.WriteLine("1. Add Student");
            Console.WriteLine("2. Display Students");
            Console.WriteLine("3. Update Student");
            Console.WriteLine("4. Delete Student");
            Console.WriteLine("5. Exit");
            Console.Write("Enter your choice: ");
            choice = int.Parse(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    Console.Write("Enter Roll Number: ");
                    int rollNo = int.Parse(Console.ReadLine());

                    Console.Write("Enter Name: ");
                    string name = Console.ReadLine();

                    Console.Write("Enter Marks: ");
                    double marks = double.Parse(Console.ReadLine());

                    sms.AddStudent(rollNo, name, marks);
                    break;

                case 2:
                    sms.DisplayStudents();
                    break;

                case 3:
                    Console.Write("Enter Roll Number to Update: ");
                    rollNo = int.Parse(Console.ReadLine());

                    Console.Write("Enter New Name: ");
                    name = Console.ReadLine();

                    Console.Write("Enter New Marks: ");
                    marks = double.Parse(Console.ReadLine());

                    sms.UpdateStudent(rollNo, name, marks);
                    break;

                case 4:
                    Console.Write("Enter Roll Number to Delete: ");
                    rollNo = int.Parse(Console.ReadLine());
                    sms.DeleteStudent(rollNo);
                    break;
            }

        } while (choice != 5);
    }
}

Complete Source Code with Output

using System;
using System.Collections.Generic;
using System.Linq;

public class Student
{
    public int RollNo { get; set; }
    public string Name { get; set; }
    public double Marks { get; set; }
}

public class StudentManagementSystem
{
    private List<Student> students = new List<Student>();

    public void AddStudent(int rollNo, string name, double marks)
    {
        students.Add(new Student { RollNo = rollNo, Name = name, Marks = marks });
    }

    public void DisplayStudents()
    {
        foreach (var student in students)
        {
            Console.WriteLine($"Roll No: {student.RollNo}, Name: {student.Name}, Marks: {student.Marks}");
        }
    }

    public void UpdateStudent(int rollNo, string newName, double newMarks)
    {
        var student = students.FirstOrDefault(s => s.RollNo == rollNo);
        if (student != null)
        {
            student.Name = newName;
            student.Marks = newMarks;
        }
    }

    public void DeleteStudent(int rollNo)
    {
        var studentToRemove = students.FirstOrDefault(s => s.RollNo == rollNo);
        if (studentToRemove != null)
        {
            students.Remove(studentToRemove);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        StudentManagementSystem sms = new StudentManagementSystem();

        int choice;

        do
        {
            Console.WriteLine("1. Add Student");
            Console.WriteLine("2. Display Students");
            Console.WriteLine("3. Update Student");
            Console.WriteLine("4. Delete Student");
            Console.WriteLine("5. Exit");
            Console.Write("Enter your choice: ");
            choice = int.Parse(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    Console.Write("Enter Roll Number: ");
                    int rollNo = int.Parse(Console.ReadLine());

                    Console.Write("Enter Name: ");
                    string name = Console.ReadLine();

                    Console.Write("Enter Marks: ");
                    double marks = double.Parse(Console.ReadLine());

                    sms.AddStudent(rollNo, name, marks);
                    break;

                case 2:
                    sms.DisplayStudents();
                    break;

                case 3:
                    Console.Write("Enter Roll Number to Update: ");
                    rollNo = int.Parse(Console.ReadLine());

                    Console.Write("Enter New Name: ");
                    name = Console.ReadLine();

                    Console.Write("Enter New Marks: ");
                    marks = double.Parse(Console.ReadLine());

                    sms.UpdateStudent(rollNo, name, marks);
                    break;

                case 4:
                    Console.Write("Enter Roll Number to Delete: ");
                    rollNo = int.Parse(Console.ReadLine());
                    sms.DeleteStudent(rollNo);
                    break;
            }

        } while (choice != 5);
    }
}

Output:

1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 1
Enter Roll Number: 101
Enter Name: Alice
Enter Marks: 95.5

1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 1
Enter Roll Number: 102
Enter Name: Bob
Enter Marks: 89.2

1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 2
Roll No: 101, Name: Alice, Marks: 95.5
Roll No: 102, Name: Bob, Marks: 89.2

Conclusion 

We've built a simple Student Management System in C# as a console application. You can expand upon this by adding more functionalities like searching for students, validating input data, or even persisting the student data to a file or database. 

Happy coding!


Comments