Skip to content

Clarify method hiding explanation in polymorphism documentation #47378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 15, 2025

The polymorphism documentation's explanation of type casting with hidden base class members was confusing readers. The original text didn't clearly distinguish between method hiding (using new) and method overriding (using override), leading to confusion about why casting affects which method is called.

Changes Made

  • Clarified method hiding vs overriding: Added explanation that new keyword creates method hiding rather than overriding
  • Explained compile-time vs runtime behavior: Clarified that with method hiding, the method called depends on the compile-time type of the variable, not the runtime type of the object
  • Added concrete example explanation: Provided clear explanation of why the same object instance calls different methods when accessed through different variable types

Before

Hidden base class members may be accessed from client code by casting the instance of the derived class to an instance of the base class. For example:

After

When you use the `new` keyword, you're creating a method that *hides* the base class method rather than *overriding* it. This is different from virtual methods. With method hiding, the method that gets called depends on the compile-time type of the variable, not the runtime type of the object.

Hidden base class members can be accessed from client code by casting the instance of the derived class to an instance of the base class. For example:

:::code language="csharp" source="./snippets/inheritance/Inheritance.cs" ID="SnippetUseNewMethods":::

In this example, both variables refer to the same object instance, but the method that gets called depends on the variable's declared type: `DerivedClass.DoWork()` when accessed through the `DerivedClass` variable, and `BaseClass.DoWork()` when accessed through the `BaseClass` variable.

This addresses the confusion expressed in the original issue where the user asked: "if i will type cast derived class object as base class then it will call base class method, but without casting it calling to the base method then what's meaning of type casting?"

Fixes #27506.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/csharp/fundamentals/object-oriented/polymorphism.md docs/csharp/fundamentals/object-oriented/polymorphism

@Copilot Copilot AI changed the title [WIP] Vague explanation of object type casting Clarify method hiding explanation in polymorphism documentation Jul 15, 2025
@Copilot Copilot AI requested a review from BillWagner July 15, 2025 15:23
Copilot finished work on behalf of BillWagner July 15, 2025 15:23
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM. I'll mark it ready for final review.

@BillWagner BillWagner requested a review from IEvangelist July 16, 2025 15:24
@BillWagner BillWagner marked this pull request as ready for review July 16, 2025 18:40
@BillWagner BillWagner requested a review from a team as a code owner July 16, 2025 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vague explanation of object type casting
2 participants