Call overrided methods - CSharp Custom Type

CSharp examples for Custom Type:virtual

Description

Call overrided methods

Demo Code

using static System.Console;
using System;/*from   w  w  w . ja v a  2s  .com*/
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Program
{
   static void Main(string[] args)
   {
      Employee aliceInEmployee = new Employee{ Name = "Alice", EmployeeCode = "1234" };
      Person aliceInPerson = aliceInEmployee;
      aliceInEmployee.WriteToConsole();
      aliceInPerson.WriteToConsole();
      WriteLine(aliceInEmployee.ToString());
      WriteLine(aliceInPerson.ToString());
   }
}
public class Employee : Person
{
   public string EmployeeCode { get; set; }
   public DateTime HireDate { get; set; }
   public new  void WriteToConsole()
   {
      WriteLine($"{Name}'s birth date is {DateOfBirth:dd/MM/yy} and hire date was {HireDate:dd/MM/yy}");
   }
   public override string ToString()
   {
      return $"{Name}'s code is {EmployeeCode}";
   }
}
public class Person : IComparable<Person>
{
   public string Name;
   public DateTime DateOfBirth;
   public List<Person> Children = new List<Person>();
   public int CompareTo(Person other)
   {
      return Name.CompareTo(other.Name);
   }
   public void WriteToConsole()
   {
      WriteLine($"{Name} was born on {DateOfBirth:dddd, d MMMM yyyy}");
   }
}

Result


Related Tutorials