dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.29k stars 5.92k forks source link

Vague explanation of object type casting #27506

Open vrdevendra opened 2 years ago

vrdevendra commented 2 years ago

[Enter feedback here]

Hi,

While reading this polymorphism i found below line of statement which says that, 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?

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: DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method.

BaseClass A = (BaseClass)B; A.DoWork(); // Calls the old method.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

BillWagner commented 2 years ago

That's a good point to note @svdevendra

That section discusses members that are not virtual. That means a new method in a derived class hides the existing method. This can be clarified.