if you are using your terminal inside ms code, you can optionally add the -r tag to open your newly created console app in the current running instance of code.
code ./pav.listExample -r
When your application opens, you should be prompted to add some Build and Debug assets, make sure to click yes.
With that all out of the way, let's create our example:
- create a base Person class that implements IComparable on the Person's age
- create an employee class that inherits form our person class
in our main
- create a list of people and fill it with both instances of Person and Employee
- sort the list, the list will use the Person's implementation of IComparable
namespace pav.ListExample
{
class Person : IComparable<Person>
{
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public Person(string Name, DateTime BirthDate) {
this.Name = Name;
this.BirthDate = BirthDate;
}
public int GetAge() {
DateTime today = DateTime.Today;
int age = today.Year - BirthDate.Year;
return BirthDate > today.AddYears(-age) ? --age : age;
}
public virtual string Display() { return $"{Name} is {GetAge()}"; }
public int CompareTo(Person? other)
{
if(other != null)
return GetAge() - other.GetAge();
return 0;
}
}
class Employee : Person
{
static int _runningID;
public int Id { get; private set; } = _runningID++;
public Employee(string Name, DateTime BirthDate) : base(Name, BirthDate) {}
public override string Display() { return $"{base.Display()} with id:{Id}"; }
}
class Program
{
static void Main(string[] args)
{
List<Person> l = new List<Person>();
var p1 = new Person("Pavel", new DateTime(1984, 01, 31));
var p2 = new Person("Tomek", new DateTime(1988, 08, 28));
var p3 = new Person("Magda", new DateTime(1984, 06, 28));
var e1 = new Employee("Marin", new DateTime(1983, 12, 04));
var e2 = new Employee("Trish", new DateTime(1987, 05, 12));
l.Add(p1);
l.Add(p2);
l.Add(p3);
l.Add(e1);
l.Add(e2);
l.Sort();
foreach (Person p in l)
Console.WriteLine(p.Display());
}
}
}