Downcasting

Treating a parent-typed variable as a specific child type; can fail, so check first.

What is Downcasting?

Downcasting is treating a parent-typed variable as a more specific child type. It can fail if the object isn't really that type, so check first.

Animal pet = GetPet();
if (pet is Dog dog)
{
    dog.Fetch(); // safe: we checked it really is a Dog
}

Level: Journeyman · More OOP terms at this level

Updated