Several combinations of c#virtual and override

Source: Internet
Author: User

Scenario 1:

Class a{public void Show ()}class b:a{public Void Show ()}

  

Compile through, there is a warning to add the new keyword in the method of B in order to hide the method of a

Compile-time binding (call method based on claim type)

Scenario 2:

Class a{public virtual void Show ()}class b:a{public Void Show ()}

Compile through, there is a warning to add the new or override keyword in the method of B, the method of A is hidden by default

Compile-time binding (call method based on claim type)

Scenario 3:

Class a{public virtual void Show ()}class b:a{public override void Show ()}

  

Compiled by

Run-time bindings (calling methods based on actual type)

Scenario 4:

Class a{public void Show ()}class b:a{public override void Show ()}

  

Compile error, the override method must be abstract, virtual, or override.

Conclusion 1: If you want to use dynamic binding (runtime binding)

Class a{public virtual void Show ()}class b:a{public override void Show ()}

  

Conclusion 2: If you want to use static bindings (compile-time binding)

Class a{public void Show ()}class b:a{public new Void Show ()}

  

It seems that can not be bookworm, or practical operation is easier to figure out:)

Several combinations of c#virtual and override

Related Article

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.