static void Main(string[] args) { try { Student s1 = new Student("X00100551", "Mark Lordan", Sex.Male); Student s2 = new Student("X00101899", "Robert Kenny", Sex.Male); StudentClass R1 = new StudentClass("Mask", "Gary Clynch"); R1.addStudent(s1); R1.addStudent(s2); Student mark = R1["X00100551"]; Console.WriteLine("Name: " + mark.Name + "\nStudent Number: " + mark.Uid); Student robbie = R1[1]; Console.WriteLine("Name: " + robbie.Name + "\nStudent Number: " + robbie.Uid); foreach(Student s in R1) { Console.WriteLine(s.ToString()); } Student missing = R1[-1]; } catch(ArgumentException ae) { Console.WriteLine(ae.Message); } Console.ReadLine(); }
public bool addStudent(Student aStudent) { bool result = false; if (studentList == null) { result = true; studentList.Add(aStudent); } else { if ((studentList.Count(s => s.Uid == aStudent.Uid)) == 1) { result = false; throw new ArgumentException("This student ID already exists"); } else { result = true; studentList.Add(aStudent); } } return result; }