Is downcasting (i.e. casting to a derived type) ALWAYS wrong?

What is your downgrade prospect? Is it ALWAYS wrong, or are there cases where it is acceptable, or even more preferable or desirable?

Is there any good measure / recommendation we can give that tells us when the demotion is “evil” and when it is “good” / “good”?

(I know a similar question exists , but this question arises from a specific case. I would like him to answer from a general point of view.)

+5
source share
3 answers

No, this is definitely not always wrong.

, , # - sender, . , , . sender Button .

- . API, - , . , Dictionary<Type, object>, , . - , , .

+8

, , - . , . API- Pre-Generics ( ), , downcasting . , , , , downcast - .

+3

"" Java pre 5.0, . . : , , . Java 5.

John Vlissides analyzes this problem a lot (aka “Laundering Type”) in his beautiful book “Conclusion of Hatching” (practically a continuation of Design Patterns ).
+1
source

All Articles