This article is a mirror article of machine translation, please click here to jump to the original article.

View: 2461|Reply: 0

[Source] C# programming uses the Override and New keywords to distinguish between them

[Copy link]
Posted on 3/23/2024 9:01:38 PM | | | |
The override modifier can be used on virtual methods, but it must be used on abstract methods. This instructs the compiler to implement it using the last defined method. Even if the method is called on a reference to the base class, it will use an implementation that overrides it.
The new modifier instructs the compiler to use the child implementation instead of the parent implementation.Any code that does not reference your class but references the parent class will be implemented using the parent class

Declare two classes: BaseClass and DerivedClass. DerivedClass inherits from BaseClass. As shown below:



Code:


In simple terms: when the parent class refers to a child class object, the method is called if it is override, the subclass method is executed; If you use new override, you are executing the parent class's method.

As shown below:



Reference:
The hyperlink login is visible.
The hyperlink login is visible.





Previous:Start the Guardian ASP.NET Core app with PM2
Next:.NET/C# reflection attribute, which can be judged by null types
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com