❔ How does class casting work?
I think code explains itself, but still. How do i cast parent class to child class?
7 Replies
(Boo)(new Foo())
will never work because a Foo
isn't a Boo
but adding
Boo
to list of Foo
works just fineYes, because a
Boo
is a Foo
myObjects
accepts Foo
objects, and you can implicitly cast a Boo
to a Foo
because one inherits from the other
But Foo
doesn't inherit from Boo
, thus you cannot cast from Foo
to Boo
can i define it somewhere?
you can make a
ToBoo
method on Foo, or add an explicit conversion
but at this point, this sounds like an $xy problemThe XY Problem
Asking about your attempted solution rather than your actual problem
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.