Override
- Override is used by derived classes to override base class methods. Call a derived class method, if you want to call the base class method with the base keyword
- Override cannot override a non-virtual method or a static method.
- Override overrides must be decorated with an abstract, override, Virtrual method.
New
- New is a derived class used to hide a base class method, and the base class method is not seen in a derived class, but it is called a method of the base class.
- If you want to hide a base class method in a derived class without using the new keyword without the new keyword, a warning will appear at compile time to indicate that if you want to hide a method in the base class, use the New keyword.
- New can hide a common method in a base class or it can be a virtual method.
- If the new method is decorated with private in a derived class, only the method in the base class is hidden in the derived class, except that the method in the base class is not hidden from the derived class;
- If a method in a base class is hidden in a derived class, in the derived class of that derived class, the hidden of the base class method is continued for that derived class.
Public classProgram {Static voidMain (string[] args) {A A=NewA (); A.test (); b b=NewB (); B.test (); A C=NewB (); C.test (); A D=NewC (); D.test (); Console.ReadLine (); } } Public classA { Public Virtual voidTest () {Console.WriteLine ("A Test ()"); } } Public classb:a {Private New voidTest () {Console.WriteLine ("b Test ()"); } } Public classc:b { Public Override voidTest () {Console.WriteLine ("C Test ()"); } }
The difference between the override of C # Fundamentals and New