尝试比较 class 个字段 C#

Trying to compare class fields C#

下面我有两个class的构造函数。学生和课程

我正在尝试比较学生 class 的 _completedCourses 值 与课程 coursecode 的值 class.

_completedCourses 是课程及其成绩的字典。我正在尝试查找未完成的课程并打印该列表。

我这样做正确吗?

public Course(string coursName, string courseCode, char passingGrade, double noOfCredits, Semester semesterOfferd, int major)
        {
            this.CourseName = coursName;
            this.CourseCode = courseCode;
            this.PassingGrade = passingGrade;
            this.NoOfCredits = noOfCredits;
            this.SemesterOfferd = semesterOfferd;
            this.prerequisiteCourses = new List<Course>();
            this._enrolledStudents = new List<Student>();
            this._major = major;
        }

 public Student(int studentID, string studentName, Status studentStatus, StudentMajor studentMajor)
        {
            this._studentID = studentID;
            this._studentName = studentName;
            this._studentStatus = studentStatus;
            this._studentMajor = studentMajor;
            this._course = new Course();
            this._completedCourses =  new Dictionary<string, char>();
            countStudent++;
        }

  public void RemainingCourses() 
        {
           var cKey = _completedCourses.ContainsKey(_course.CourseCode);
            Console.WriteLine("Remaining Courses");
            if (cKey.Equals(_course.CourseCode))
            {

                foreach (KeyValuePair<string, char> count in _completedCourses)
                {
                    {
                        {
                            Console.WriteLine(count.Key + " " + count.Value);
                            //          Console.WriteLine("Course "  + count.Value);
                            count.ToString();
                        }
                    }
                }
            }
        }

更新!!!

我的驱动程序中的以下代码行 class 实现了我想要的

Console.WriteLine("Enter Student ID ");
                    input = Convert.ToInt32(Console.ReadLine());
                    Console.Clear();
                    if (input.Equals(id.StudentID)) {
                        id.DisplayCompletedCourse();
                        foreach (var sub in isdCourses) {
                            var cKey = id.CompletedCourses.ContainsKey(sub.CourseCode);
                            if (!cKey)
                            { 
                                Console.WriteLine(sub.CourseCode);
                            }
                        }
                    }

ContainsKey方法returns一个布尔值(true/false),所以在你的if条件下:

if (cKey.Equals(_course.CourseCode))

cKey 是一个布尔值,但 _course.CourseCode 是一个字符串,所以这永远不会是真的,因此这永远不会在 if 块中。

尝试这样重写:

public void RemainingCourses() 
{
    var cKey = _completedCourses.ContainsKey(_course.CourseCode);
    Console.WriteLine("Remaining Courses");
    if (cKey) // if cKey is "true", then the dictionary contains the CourseCode
    {
        foreach (KeyValuePair<string, char> count in _completedCourses)
        {
            Console.WriteLine(count.Key + " " + count.Value);
        }
    }
}

附带说明,请注意格式,您添加了不必要的大括号。此外,这可以使用 linq 进行简化,但我将其尽可能接近原始代码。