Managing Data with Lists and Classes in C#

Hello, Space Explorer! Today, we’re going to discuss a practical and essential topic in C#: managing data using lists and classes. To practice this concept, we will build a simple Student Management System. Specifically, we will create a class that stores students and their grades. This hands-on approach will help us understand how lists and objects can be used effectively in real-world applications. Are you excited? Great, let's dive in!

Introducing Methods to Implement

To accomplish our task, we need to implement three primary methods within our class:

  1. AddStudent(string name, int grade): This method allows us to add a new student and their grade to our list. If the student is already on the list, their grade will be updated.
  2. GetGrade(string name): This method retrieves the grade for a student given their name. If the student is not found, it returns null.
  3. RemoveStudent(string name): This method removes a student from the list by their name. It returns true if the student is successfully removed and false if the student is not found.

Does that sound straightforward? Fantastic, let's break it down step-by-step.

Implementing the Solution Step-by-Step

Let’s start by defining our StudentManager class, which will use a list of objects to manage students and their grades. We also will define a Student class to represent individual student data.

using System;
using System.Collections.Generic;

public class StudentManager
{
    private List<Student> students;

    public StudentManager()
    {
        students = new List<Student>();
    }
}

public class Student
{
    public string? Name { get; set; }  // Make Name nullable
    public int Grade { get; set; }
}

class Program
{
    static void Main()
    {
        StudentManager manager = new StudentManager();
        Console.WriteLine("StudentManager instance created.");
    }
}
Step 1: Implementing AddStudent

The AddStudent method checks if a student already exists in our list. If so, their grade is updated; otherwise, the student is added to the list.

using System;
using System.Collections.Generic;

public class StudentManager
{
    private List<Student> students;

    public StudentManager()
    {
        students = new List<Student>();
    }

    public void AddStudent(string name, int grade)
    {
        foreach (var student in students)
        {
            if (student.Name == name)
            {
                student.Grade = grade;
                Console.WriteLine(name);
                return;
            }
        }
        students.Add(new Student { Name = name, Grade = grade });
        Console.WriteLine(name);
    }
}

public class Student
{
    public string? Name { get; set; }
    public int Grade { get; set; }
}

class Program
{
    static void Main()
    {
        StudentManager manager = new StudentManager();
                
        // Test AddStudent method
        manager.AddStudent("Alice", 90);
        manager.AddStudent("Bob", 85);
        manager.AddStudent("Alice", 95);
    }
}

Let's break it down:

  • Using a foreach loop, we iterate through students.
  • If we find an object where the Name property matches the given name, we update the grade.
  • If not found, we append a new Student object { Name = name, Grade = grade } to our list.

Why do we need to check if the student already exists before appending? Preventing duplicate entries and ensuring data consistency is key!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal