Use member fields from parent class - CSharp Custom Type

CSharp examples for Custom Type:Inheritance

Description

Use member fields from parent class

Demo Code

using static System.Console;
using System;//  ww  w.  ja  v a 2 s .  c om
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Program
{
   static void Main(string[] args)
   {
      Employee e1 = new Employee
      {
         Name = "D",
         DateOfBirth = new DateTime(1990, 7, 28)
      };
      e1.WriteToConsole();
   }
}
public class Employee : Person
{
   public string EmployeeCode { get; set; }
   public DateTime HireDate { get; set; }
   public 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);
   }
}

Result


Related Tutorials