Use new to mark override function - CSharp Custom Type

CSharp examples for Custom Type:virtual

Description

Use new to mark override function

Demo Code

using static System.Console;
using System;// w  w  w.  j  av a  2s . c o m
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.EmployeeCode = "1234";
        e1.HireDate = new DateTime(2014, 11, 23);
        WriteLine($"{e1.Name} was hired on {e1.HireDate:dd/MM/yy}");
    }
}
public class Employee : Person
{
    public string EmployeeCode { 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 DateTime HireDate { get; set; }
    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}'s birth date is {DateOfBirth:dd/MM/yy} and hire date was {HireDate:dd/MM/yy}");
    }
}

Result


Related Tutorials