Managing Student Enrollments Using C# Dictionary and HashSet

In this lesson, we'll learn how to manage student enrollments using C#'s Dictionary and HashSet collections. These data structures allow efficient data organization and access, which are essential for building an enrollment system. We'll implement methods to handle enrolling and unenrolling students, checking enrollment status, and listing students in a course. This practical approach will enhance your understanding of these advanced structures. Now, let's dive into the methods we need to implement.

Introducing Methods to Implement

Here are the methods we need to implement in our enrollment system:

  • Enroll(string student, string course): This method adds a student to a course. If the student is already enrolled, it does nothing.
  • Unenroll(string student, string course): This method removes a student from a course. It returns true if the student was enrolled and has now been removed. Otherwise, it returns false. If, after unenrolling the student, the course becomes empty (no one is enrolled there), remove the course as well.
  • IsEnrolled(string student, string course): This method checks if a student is enrolled in a course. It returns true if the student is enrolled and false otherwise.
  • ListStudents(string course): This method returns a List<string> of all students enrolled in a given course. If no students are enrolled, it returns an empty List<string>.

Let's look at how to implement each of these methods step by step.

Step 1: Define the Class

We'll start by defining our class and then add each method one by one.

First, we define our EnrollmentSystem class:

This code initializes an EnrollmentSystem class with a Dictionary named enrollments that maps courses to sets of students.

Step 2: Implement Enroll Method

Next, we implement the Enroll method:

In the Enroll method:

  1. We check if the course exists in the Dictionary. If it doesn't, we initialize a new HashSet for that course.
  2. We then add the student to the set if they are not already enrolled.
Step 3: Implement Unenroll Method

Let's move on to the Unenroll method:

This method first checks whether the course exists in the enrollments and whether the student is enrolled in that course. If they are, it removes the student from the course. If the course set becomes empty after removal, it deletes the course from the Dictionary. The method returns true if the student was successfully unenrolled and false otherwise.

Step 4: Implement IsEnrolled Method

Next, let's implement the IsEnrolled method:

This method checks whether the specified course exists in the enrollments and whether the student is enrolled in that course. If both conditions are met, it returns true; otherwise, it returns false.

Step 5: Implement ListStudents Method

Finally, let's implement the ListStudents method:

The ListStudents method returns a List<string> of students enrolled in the given course. If the course is not in the Dictionary, it returns an empty List<string>.

Lesson Summary

In today's lesson, we learned how to manage student enrollments using C# Dictionary and HashSet objects. We implemented methods to enroll and unenroll students, check enrollments, and list students in a course. This task provided a practical way to reinforce your understanding of dictionaries and sets in C#.

I encourage you to move on to the practice to undertake similar challenges to deepen your understanding. Keep experimenting and honing your skills. Happy coding!

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