Differences between C # new and override

Source: Internet
Author: User

In C #, new and override are two frequently used keywords in inheritance, but they are often easy to confuse the functions of these two keywords.

 

New

C # The new Keyword indicates hiding. It indicates that an attribute or function with the new keyword will hide the Same Name attribute or function of the base class for this class and the inherited class.

Public class
{
Public virtual void Method ()
{
Console. WriteLine ("This Method in Class! ");
}
}

Public class B:
{
Public new void Method ()
{
Console. WriteLine ("This Method in Class B! ");
}
}

For the above example, if you run A a = new B (); a. Method ();, This Method in Class !, This is because class B inherits from class A. Now the Method function in class B hides the Method in class A, so from the perspective of class B (including the subclass inherited from class B), the Method in class is B. method, the Method of A is invisible. However, from the perspective of A, if B, A only recognizes the Method function inherited from Class A in Class B, it is invisible to Method in Class B, so A a = new B ();. method (); equivalent to calling the Method function inherited from A in Class B

 

 

Override

In C #, the override keyword indicates rewriting. attributes or functions with the override keyword will completely overwrite the virtual attributes or virtual functions of the base class with the same name, make the virtual attributes and virtual functions of the base class invisible throughout the inheritance chain (except for calling with the base keyword in the subclass ).

Public class
{
Public virtual void Method ()
{
Console. WriteLine ("This Method in Class! ");
}
}

Public class B:
{
Public override void Method ()
{
Console. WriteLine ("This Method in Class B! ");
}
}

For the above example, if you run A a = new B (); a. Method ();, This Method in Class B is output !, Because the Method function of class B completely overwrites the virtual function Method of the same name of the base class, the Method function seen in the entire inheritance chain is the Method in Method B, so from the perspective of A, B, the Method function seen by a is also the Method in B, because the Method in A is completely overwritten by B.

However, if you want to call the Method function of A in the object of B, you can use the base keyword, such

Public class
{
Public virtual void Method ()
{
Console. WriteLine ("This Method in Class! ");
}
}

Public class B:
{
Public override void Method ()
{
Base. Method ();
}
}

A a = new B (); a. Method (); This Method in Class !, Base. Method (); indicates the Method inherited from base class A in Class B.

 

New, override, and interface

When an interface inherits from each other, it also hides the attributes or functions of the same name as the basic interface. However, for an interface, hiding does not work for the basic interface, all the attributes and functions in the interface are declared. They all point to the same name implementation function in the class that implements the interface, when you call attributes and functions of an interface, you can call the functions with the same name and attributes first visible from top to bottom in the implementation class:

Public interface IA
{
Void Method ();
}

Public interface IB: IA
{
New void Method ();
}

Public class IClass: IB
{
Public void Method ()
{
Console. WriteLine ("This Method in Class IClass! ");
}
}

Public class ISubClass: IClass
{
Public new void Method ()
{
Console. WriteLine ("This Method in Class ISubClass! ");
}
}

IA ia = new ISubClass (); ia. Method (); output This Method in Class IClass! For ia, the first implementation function with the same name in the inheritance chain is the Method function of IClass class.

 

Slightly modify the above example:

Public interface IA
{
Void Method ();
}

Public interface IB: IA
{
New void Method ();
}

Public class IClass: IB
{
Public virtual void Method ()
{
Console. WriteLine ("This Method in Class IClass! ");
}
}

Public class ISubClass: IClass
{
Public override void Method ()
{
Console. WriteLine ("This Method in Class ISubClass! ");
}
}

IA ia = new ISubClass (); ia. Method (); output This Method in Class ISubClass! Because the Method functions in the inheritance chain are overwritten by the ISubClass Method, the implementation function with the same name is the Method function of ISubClass in the inheritance chain for ia.

 

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.