The difference between new,override in C #

Source: Internet
Author: User

Recently I briefly review the following in C # to inherit this feature, the need to use the keyword, which has some key points, specially sorted out, convenient for everyone to check. This issue was also mentioned in a written and interview session at Microsoft:
First, in C #, the New keyword is used very frequently, there are 3 main functions:


A) used as an operator to create an object and call the constructor.


b) as a modifier.


c) To constrain the type of a parameter that may be used as a type parameter in a generic declaration.


In this article, you will only describe the role of new as a modifier, and when used as a modifier, the new keyword can hide a method of a base class in a derived class, and it is said that the method of using a derived class is to invoke a method that is newly defined by the New keyword, not a method of the base class. It is also possible to hide the base class method without using the New keyword, and the compiler will see a warning that if you intentionally hide the method of the base class, use the new keyword modifier.


One of the things to note here is that the two keywords for new and override are mutually exclusive. Can not be used at the same time.


The override keyword is mainly to provide a new implementation of the base class method for the derived class, overriding the base class method must have the same signature as the override method, this keyword cannot be used to override non-virtual and static methods, and the key used in its companion is virtual, abstract, Override. At the same time, the override method can not modify the accessibility of the virtual method, and the override method must have the same access modifier as the virtual method, and cannot be modified with the modifier new, static, virtual, or abstract Override method.


Here is a small demo showing the essential differences between new and override:





Class test{static void Main (string[] args) {Base ra = new ClassA (); Base RB = new ClassB (); Ra.prinf (); Rb.prinf ();}} Abstract public class base{public virtual void Fuck () {Console.WriteLine ("Fuck in Base");}} public class a:base{public override void Fuck () {Console.WriteLine ("Fuck in Class A");}} public class b:base{public new void Fuck () {Console.WriteLine ("Fuck in Class B");}}



Output Result:


Fuck in Calss A

Fuck in class B




The virtual keyword allows you to override these objects in a derived class by default, the method is non-virtual and cannot be overridden, and the virtual keyword cannot be used with static, abstract, private, override. The virtual keyword is also closely related to the override, and if you want to implement the virtual method you must use the override or New keyword (the mechanism of new and override is different).


Summarize:


The 1.NEW keyword is primarily used to distinguish between derived and base class method selection problems, by hiding the base class method, to make the compiler call the correct method. For example, a subclass reference invokes a child class, and the parent class reference invokes the parent class.


2.Override is primarily used to override the base class's methods and virtual methods. A reference to a subclass or a parent class, because it points to a subclass object, so it is called a member of the child class.







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The difference between new,override in C #

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.