C # Virtual methods

Source: Internet
Author: User

  • The base class method must be defined as virtual.

  • If a method in a derived class does not have a new or override keyword in front of it, the compiler will issue a warning that the method will perform the action as if it were a new keyword.

  • If a method in a derived class is preceded by a new keyword, the method is defined as independent of the method in the base class.

  • If a method in a derived class is preceded by a override keyword, the object of the derived class will call the method instead of calling the base class method.

  • You can call a base class method from a derived class by using a base keyword.

  • override, virtual and new keywords can also be used in properties, indexers, and events.

    namespacevirtual Method { Public classClass1 { Public Virtual voidSay () {Console.WriteLine ("Hello world!"); }    }     Public classClass2:class1 { Public Override voidSay () {Base.            Say (); Console.WriteLine ("Hello, world! "); }    }    classProgram {Static voidMain (string[] args) {Class2 C2=Newvirtual methods. Class2 (); C2.        Say (); }    }}

C # Virtual methods

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.