The difference between overriding (override) and overwrite (new) in C # learning note (--c#)

Source: Internet
Author: User

Description (2017-7-17-23:04:45):

Original:

The difference between overriding (override) and overwrite (new) in C #

Rewrite

The method of virtual modification with the keyword is called the virtual method. You can declare a method with the same name in the subclass with Override, which is called "overriding." The corresponding method is not modified by virtual, we call it a real method.
Rewriting alters the functionality of the parent class method.
Look at the following demo code:#region Rewrite

public class C1
{
Public virtual string GetName ()
{
Return "Uncle Xiang";
}
}

public class C2:c1
{
public override string GetName ()
{
return "Xiangshu";
}
}

C1 C1 = new C1 ();
Console.WriteLine (C1. GetName ());//output "Uncle Xiang"

C2 C2 = new C2 ();
Console.WriteLine (C2. GetName ());//Output "Xiangshu"

Here's the point!

C1 C3 = New C2 ();
Console.WriteLine (C3. GetName ());//Output "Xiangshu"

#endregion

cover
A method defined in a subclass with the same name as the parent class in the New keyword adornment is called overwrite.
Overwriting does not change the functionality of the parent class method. Look at the following demo code:#region Coverage

public class C1
{
public string GetName ()
{
Return "Uncle Xiang";
}
}

public class C2:c1
{
Public new string GetName ()
{
return "Xiangshu";
}
}

C1 C1 = new C1 ();
Console.WriteLine (C1. GetName ());//output "Uncle Xiang"

C2 C2 = new C2 ();
Console.WriteLine (C2. GetName ());//Output "Xiangshu"

Focus on this, and the above rewrite as a comparison

C1 C3 = New C2 ();
Console.WriteLine (C3. GetName ());//output "Uncle Xiang"

#endregion

Summarize

1: Neither rewriting nor overwriting will affect the functionality of the parent class itself (nonsense, yes, unless the code is changed).

2: When creating a parent class with a subclass, such as C1 C3 = New C2 (), overriding (override) alters the function of the parent class, that is, the function of the child class is called, while overwrite (new) does not, still calls the parent class feature.

3: Virtual methods, real methods can be overridden (new), abstract methods, interfaces are not available.

4: Abstract method, interface, method marked as virtual can be overridden (override), the real method is not possible.

5: The rewrite uses the frequency is relatively high, realizes the polymorphism, the coverage uses the frequency is relatively low, is used for inherits the class which previously cannot modify.

The difference between overriding (override) and overwrite (new) in C # learning note (--c#)

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.