using System.Collections.Generic;
class Student : IComparable<Student>
public Student(string name, string lastname)
this.lastname = lastname;
public int CompareTo(Student other)
result = this.lastname.CompareTo(other.lastname);
result = this.name.CompareTo(other.name);
class RangeException : Exception
public override string Message
return $"Expected mark in range[0..100] but received {mark}";
public RangeException(int mark)
class ExistExceptions : Exception
public override string Message
return $"Student {name} {lastname} is already exists";
public ExistExceptions(string name, string lastname)
this.lastname = lastname;
private SortedDictionary<Student, int> studentsCollection = new SortedDictionary<Student, int>();
public void AddStudent(Student st, int mark)
studentsCollection.Add(st, mark);
public void AddStudents(int n)
for (int i = 0; i < n; i++)
Console.WriteLine("Введите имя студента:");
string name = Console.ReadLine();
Console.WriteLine("Введите фамилию студента:");
string lastname = Console.ReadLine();
Console.WriteLine("Введите оценку студента:");
int mark = Int32.Parse(Console.ReadLine());
if(mark > 100 || mark < 0)
throw new RangeException(mark);
foreach(Student st in studentsCollection.Keys)
if (st.name == name && st.lastname == lastname)
throw new ExistExceptions(name, lastname);
Student student = new Student(name, lastname);
AddStudent(student, mark);
Console.WriteLine($"Exception: {e.Message}");
catch (ExistExceptions e)
Console.WriteLine($"Exception: {e.Message}");
public void ListStudents()
foreach (KeyValuePair<Student, int> keyValue in studentsCollection.OrderBy(pair => pair.Key))
Console.WriteLine($"{keyValue.Key.name} {keyValue.Key.lastname} | {keyValue.Value} ");
public static void Main(string[]args)
DictionaryStudents ds = new DictionaryStudents();