c hides the use of base class methods

  • 2020-05-26 10:00:34
  • OfStack

When a derived class and a base class have the same methods (the same method name, the same argument list, and the same return value), the derived class's methods can hide the base class's methods. That is, you can create the same method in a derived class as the base class method, but perform it differently and use the new keyword.


class program
{
    static void Main(string[] args)
    {
      B b=new B();
      b.F();
      A a=b;
      a.F();
      Console.ReadKey();
    }
  }
  class A
  {
    public void F()
    {
      Console.WriteLine("A.F");
    }
  }
  class B:A
  {
    new public void F() // hidden A In the class F methods 
    {
      Console.WriteLine("B.F");
    }
  }
 


Related articles: