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

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

Patricia Arquette
Patricia ArquetteOriginal
2025-01-28 14:05:09229browse

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

C# method hidden and rewritten: detailed explanation

In C#, the derived class can operate the base class method in two different ways: method hiding and method rewriting. The difference between understanding these two concepts is essential for effective code maintenance and inheritance.

Method hides

When a class declares a new method of the same name as the foundation Chinese method, but there is no explicit statement as the rewriting method, the method will be hidden. The method of the new statement effectively hides the implementation of the base class and provides its own independent implementation.

Example:

When CLB calls CLB.FOO () in the instance of class B, it executes the implementation of Class B and outputs "1". However, when we convert CLB into a class A, the caller accesses the base class implementation of FOO () and outputs "5".

Rewriting
<code class="language-csharp">class A {
   public int Foo(){ return 5;}
}
class B : A{
   public new int Foo() { return 1;} //隐藏
}</code>

Different from hidden, the method rewrite the implementation of the new implementation method in the derivative class. This is achieved by using the Override keyword.

Example:

When the instance of class B calls CLB.BAR (), it executes the rewriting implementation in Class B and outputs "1". Converting CLB for mandatory category A will not affect behavior, because calling and re -reality is always called.

Main differences

<code class="language-csharp">class A {
   public virtual int Bar(){return 5;}
}
class B : A{
   public override int Bar() {return 1;} //重写
}</code>

Hidden the foundation method:

Hidden in the derivative class created a new method, which hides the implementation of the base class. The rewriting replaced the realization of the base class.

Rewriting requires keywords: Rewriting needs to use Override keywords to explicitly indicate the expected replacement.

    Hidden Destruction inheritance:
  • By hiding the implementation of the base class, hiding may destroy the inheritance chain. Rewriting the inheritance relationship. The hidden method is not polymorphism:
  • The hidden method is not affected by the multi -state mechanism (such as dynamic binding). The rewriting method shows polymorphism and makes different responses according to the actual object type.

The above is the detailed content of What's the Difference Between Method 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