Home >Backend Development >C++ >What's the Difference Between Shadowing and Overriding in C#?

What's the Difference Between Shadowing and Overriding in C#?

Barbara Streisand
Barbara StreisandOriginal
2025-01-28 14:08:10514browse

What's the Difference Between Shadowing and Overriding in C#?

In -depth understanding of the hidden and rewriting in C#

In object -oriented programming, hiding and rewriting are the key concepts that affect code behavior. This article will explore the differences between the two in the inheritance system of the C#class.

Suppose we have a base "a", including two methods "FOO" and "Bar". The "FOO" method is declared for Public, and the "Bar" method declaration is Public Virtual, allowing the derived class to rewrite its implementation. Now, consider a derivative "B" inherited from "A".

Hidden

In hidden, the derived class "B" defines a method called "FOO", and its name and parameter list are the same as the base method. However, in this case, the implementation of the method in the "B" did not rewrite the base class. It creates a new independent method in the class "B" to hide the base method.

For example, in the code fragment provided:

The "NEW" keywords before the

Class "B" Chinese method indicate that the "B" type object is accessed by the "B" type. The "FOO" method is effectively hidden.
<code class="language-c#">public new int Foo() { return 1;}     //隐藏</code>

Rewriting

On the other hand, when the rewriting occurs in a method of the derivative class, the method is the same as the virtual method or abstraction method in the base class. In this case, the implementation of the derived class has explicitly replaced the implementation of the base class and became the default behavior of the derived instance.

In a given example:

Because the "bar" statement in the base "a" is Virtual, the "Override" keyword allows the class "B" to rewrite it and provide its own behavior.

<code class="language-c#">public override int Bar() {return 1;} //重写</code>
Impact

When converting the derived instances into a base class reference, hiding may lead to accidental behavior. In this case, the hidden method in the base class may be called instead of the rewriting method in the derivative class, which may cause errors during runtime.

However, the rewriting follows the inheritance hierarchical structure and ensures that the appropriate methods defined in these classes are implemented in the appropriate method of the derived class, so as to ensure consistent and expected behaviors.

The above is the detailed content of What's the Difference Between Shadowing and Overriding in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn