SortedSetnumbers = new SortedSet (); numbers.Add(6); numbers.Add(3); numbers.Add(9); numbers.Add(2); numbers.Add(8); foreach (int number in numbers) { Console.WriteLine(number); }
class Person : IComparableThis code creates a SortedSet of Person objects. Each Person object has a Name and Age property. The Person class implements the IComparable interface, which allows the SortedSet to sort the objects by their Age property. The code then loops through the SortedSet and prints out each Person's Name and Age in ascending order of Age. Package library: System.Collections.Generic.{ public string Name { get; set; } public int Age { get; set; } public int CompareTo(Person other) { return this.Age.CompareTo(other.Age); } } SortedSet people = new SortedSet (); people.Add(new Person { Name = "John", Age = 25 }); people.Add(new Person { Name = "Mary", Age = 20 }); people.Add(new Person { Name = "Mike", Age = 30 }); foreach (Person person in people) { Console.WriteLine(person.Name + " - " + person.Age); }