Differences between virtual, new and override in c #

Source: Internet
Author: User

Class A has the public virtual void test () method ()
Class B is inherited from Class A and has the public new void test () method ()

Or Class B inherits from Class A and has the method public override to say void test ()

If you use override, the system will find the test () of its substantive class, whether it is calling test () in Class A or Class B ();
If New is used, you can call test () of the base class through type conversion ();

The following is the case of override:
A a1 = new B ();
A1.test (); // call test () in B. The system automatically recognizes instances where a1 is B.
(A) B). test (); // same as above

The following is the case of new:
A a1 = new B ();
A1.test (); // call test () in ();
(A) B). test (); // same as above

In addition, virtual is a virtual method, that is, it is waiting to be overwritten, and override is the virtual method after rewriting. As for the new method, the parent class has the same method (the same name and the same parameter return value), but the subclass uses its own method, instead of the parent class, it is new, it means I use my own

If you do not understand it, it doesn't matter. The following is another example on the Internet:

In C #, if you use the virtual keyword when declaring a method, you can use the override or new keyword in the derived class to discard or ignore it. if you use the virtual keyword in the parent class and do not use the override or new keyword in the derived class, but directly reference a method with the same name, the compiler will report an error, the new method is used to ignore the methods in the derived class. the following example can help you understand:
000: // Versioning \ versioning. cs
001: public class MyBase
002 :{
003: public virtual string Meth1 ()
004 :{
005: return "MyBase-Meth1 ";
006 :}
007: public virtual string Meth2 ()
008 :{
009: return "MyBase-Meth2 ";
010 :}
011: public virtual string Meth3 ()
012 :{
013: return "MyBase-Meth3 ";
014 :}
015 :}
016:
017: class MyDerived: MyBase
018 :{
019: public override string Meth1 ()
020 :{
021: return "MyDerived-Meth1 ";
022 :}
023: public new string Meth2 ()
024 :{
025: return "MyDerived-Meth2 ";
026 :}
027: public string Meth3 () // The system will have a warning here and the method Meth3 () will be hidden ()
028:
029:
030 :{
031: return "MyDerived-Meth3 ";
032 :}
033:
034: public static void Main ()
035 :{
036: MyDerived mD = new MyDerived ();
037: MyBase mB = (MyBase) mD;
038:
039: System. Console. WriteLine (mB. Meth1 ());
040: System. Console. WriteLine (mB. Meth2 ());
041: System. Console. WriteLine (mB. Meth3 ());
042 :}
043 :}

Output:

MyDerived-Meth1
MyBase-Meth2
MyBase-Meth3
Obviously, the output of the last two new keywords is the output of the methods in the parent class. Therefore, we can see that if the new keyword is used in previous versions, the previous method is used instead of the content of the current method. the role of virtual methods is the opposite. If there is such a method in the parent class, use the method content I have written to get rid of the previous ones! However, the use of new here seems not very good, people misunderstand.

If you delete row 037th, change all mB in row 039-041 to mD, and change the output:

MyDerived-Meth1
MyDerived-Meth2
MyDerived-Meth3

What does this mean? It means that the objects of the derived class will take effect only when they are reshaped by the parent class. this is indeed a bit difficult to understand. You can only figure out the relevant authorities by yourself!

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.