Lesson 1
Managing Data with Lists and Classes in C#
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.

C#
1using System; 2using System.Collections.Generic; 3 4public class StudentManager 5{ 6 private List<Student> students; 7 8 public StudentManager() 9 { 10 students = new List<Student>(); 11 } 12} 13 14public class Student 15{ 16 public string? Name { get; set; } // Make Name nullable 17 public int Grade { get; set; } 18} 19 20class Program 21{ 22 static void Main() 23 { 24 StudentManager manager = new StudentManager(); 25 Console.WriteLine("StudentManager instance created."); 26 } 27}
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.

C#
1using System; 2using System.Collections.Generic; 3 4public class StudentManager 5{ 6 private List<Student> students; 7 8 public StudentManager() 9 { 10 students = new List<Student>(); 11 } 12 13 public void AddStudent(string name, int grade) 14 { 15 foreach (var student in students) 16 { 17 if (student.Name == name) 18 { 19 student.Grade = grade; 20 Console.WriteLine(name); 21 return; 22 } 23 } 24 students.Add(new Student { Name = name, Grade = grade }); 25 Console.WriteLine(name); 26 } 27} 28 29public class Student 30{ 31 public string? Name { get; set; } 32 public int Grade { get; set; } 33} 34 35class Program 36{ 37 static void Main() 38 { 39 StudentManager manager = new StudentManager(); 40 41 // Test AddStudent method 42 manager.AddStudent("Alice", 90); 43 manager.AddStudent("Bob", 85); 44 manager.AddStudent("Alice", 95); 45 } 46}

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!

Step 2: Implementing GetGrade

The GetGrade method searches for a student in the list by name and returns their grade.

C#
1using System; 2using System.Collections.Generic; 3 4public class StudentManager 5{ 6 private List<Student> students; 7 8 public StudentManager() 9 { 10 students = new List<Student>(); 11 } 12 13 public void AddStudent(string name, int grade) 14 { 15 foreach (var student in students) 16 { 17 if (student.Name == name) 18 { 19 student.Grade = grade; 20 Console.WriteLine(name); 21 return; 22 } 23 } 24 students.Add(new Student { Name = name, Grade = grade }); 25 Console.WriteLine(name); 26 } 27 28 public int? GetGrade(string name) 29 { 30 foreach (var student in students) 31 { 32 if (student.Name == name) 33 { 34 return student.Grade; 35 } 36 } 37 return null; 38 } 39} 40 41public class Student 42{ 43 public string? Name { get; set; } 44 public int Grade { get; set; } 45} 46 47class Program 48{ 49 static void Main() 50 { 51 StudentManager manager = new StudentManager(); 52 53 // Test AddStudent method 54 manager.AddStudent("Alice", 90); 55 manager.AddStudent("Bob", 85); 56 manager.AddStudent("Alice", 95); 57 58 // Test GetGrade method with formatted output 59 Console.WriteLine($"Alice's grade: {manager.GetGrade("Alice")?.ToString() ?? "Not found"}"); 60 Console.WriteLine($"Bob's grade: {manager.GetGrade("Bob")?.ToString() ?? "Not found"}"); 61 Console.WriteLine($"Charlie's grade: {manager.GetGrade("Charlie")?.ToString() ?? "Not found"}"); 62 } 63}

Let's consider the approach:

  • We iterate through students.
  • If we find an object where the Name property matches the given name, we return the Grade property.
  • If we exhaust the list without finding the student, we return null.

Can you think of situations where a student might not be found? Right, they might be new students or there might be potential typos in the name.

Step 3: Implementing RemoveStudent

The RemoveStudent method removes a student from the list by their name and returns whether the operation was successful.

C#
1using System; 2using System.Collections.Generic; 3 4public class StudentManager 5{ 6 private List<Student> students; 7 8 public StudentManager() 9 { 10 students = new List<Student>(); 11 } 12 13 public void AddStudent(string name, int grade) 14 { 15 foreach (var student in students) 16 { 17 if (student.Name == name) 18 { 19 student.Grade = grade; 20 Console.WriteLine(name); 21 return; 22 } 23 } 24 students.Add(new Student { Name = name, Grade = grade }); 25 Console.WriteLine(name); 26 } 27 28 public int? GetGrade(string name) 29 { 30 foreach (var student in students) 31 { 32 if (student.Name == name) 33 { 34 return student.Grade; 35 } 36 } 37 return null; 38 } 39 40 public bool RemoveStudent(string name) 41 { 42 for (int i = 0; i < students.Count; i++) 43 { 44 if (students[i].Name == name) 45 { 46 students.RemoveAt(i); 47 return true; 48 } 49 } 50 return false; 51 } 52} 53 54public class Student 55{ 56 public string? Name { get; set; } 57 public int Grade { get; set; } 58} 59 60class Program 61{ 62 static void Main() 63 { 64 StudentManager manager = new StudentManager(); 65 66 // Test AddStudent method 67 manager.AddStudent("Alice", 90); 68 manager.AddStudent("Bob", 85); 69 manager.AddStudent("Alice", 95); 70 71 // Test GetGrade method with formatted output 72 Console.WriteLine($"Alice's grade: {manager.GetGrade("Alice")?.ToString() ?? "Not found"}"); 73 Console.WriteLine($"Bob's grade: {manager.GetGrade("Bob")?.ToString() ?? "Not found"}"); 74 Console.WriteLine($"Charlie's grade: {manager.GetGrade("Charlie")?.ToString() ?? "Not found"}"); 75 76 // Test RemoveStudent method 77 Console.WriteLine(manager.RemoveStudent("Alice") ? "Alice removed" : "Alice not found"); 78 Console.WriteLine(manager.RemoveStudent("Charlie") ? "Charlie removed" : "Charlie not found"); 79 80 // Confirm removal 81 Console.WriteLine($"Alice's grade after removal: {manager.GetGrade("Alice")?.ToString() ?? "Not found"}"); 82 } 83}

Here’s what happens:

  • We iterate through students and check for a matching student name.
  • If a match is found, we delete the entry using RemoveAt and return true.
  • If no match is found by the end of the loop, we return false.

This method checks for presence before deletion, ensuring we only attempt to delete valid entries. Why is this check important? Yes, it prevents errors and helps maintain a reliable state.

Summary

In this lesson, we created a StudentManager class to manage students and their grades using C# objects and lists. We implemented three essential methods — AddStudent, GetGrade, and RemoveStudent. Each method illustrated key programming paradigms, including iteration, condition checking, and simple data manipulation.

We also demonstrated how to instantiate the StudentManager class and invoke its methods, illustrating how these operations can be used in practice.

By completing this exercise, you have fortified your knowledge of using objects and lists in practical applications. I encourage you to continue practicing these concepts by extending the class with new features or experimenting with similar problems. Always remember — practice makes perfect! Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.