The difference between the override of C # Fundamentals and New

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.