Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between hiding, rewriting and overloading in C #

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the difference between hiding, rewriting and reloading in C#". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn "what is the difference between hiding, rewriting and reloading in C#"!

Definitions of C # overloading, C # rewriting and C # hiding

C # overloading: occurs in the same scope (such as in a class) and defines a series of methods with the same name, but the parameter list of the method is different. Only in this way can you decide which one to call by passing different parameters. Different types of return values cannot constitute overloading.

C # rewriting: occurs when inheritance redefines the method in the parent class in the subclass, and the method in the subclass is the same as the method in the parent class

For example, the base class method is declared as virtual (virtual method), and the derived class uses override to declare the override of this method.

C # hiding: the base class method is not declared (the default is a non-virtual method). Use new to declare the hiding of this method in the derived class.

When C# is overloaded, select the method to be called according to the parameters

When C # is rewritten, access to the subclass of the parent class calls the override method of the subclass

When C# is hidden, accessing the parent class calls the method of the parent class and the method of the subclass subclass.

Example of C # new:

Using System; class A {public void F () {Console.WriteLine ("A.F");} class B: a {new public void F () {Console.WriteLine ("B.F") }} class Test {static void Main (string [] args) {BB = new B (); b.F (); AA = b; a.F ();}}

Output as

B.F

A.F

C # rewrite virtual (virtual method) example

Using System; class A {public virtual void F () {Console.WriteLine ("A.F");} class B: a {public override void F () {Console.WriteLine ("B.F") }} class Test {static void Main () {BB = new B (); b.F (); AA = b; a.F ();}}

Output as

B.F

B.F

Add: rewriting override is generally used for method rewriting of interface implementation and inheritance classes, it should be noted.

1. The logo of the covered method must exactly match that of the covered method in order to achieve the effect of coverage.

2. The return value of the overridden method must be the same as that of the overridden method

3. The exception thrown by the overridden method must be consistent with the exception thrown by the overridden method, or its subclass

4. The overridden method cannot be private, otherwise a new method is defined in its subclass and it is not overridden.

Thank you for your reading, the above is "what is the difference between hiding, rewriting and reloading in C#". After the study of this article, I believe you have a deeper understanding of what is the difference between hiding, rewriting and reloading in C#. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report