I assume the types in your question are sealed. In this case, I would just use an object parent and use as on the output. (Using as may have a higher performance impact than checking a flag, but ... not a problem in anything I did, and it can also be used in null protection.)
Video video = null; if ((video = parent as Video) != null) { // know we have a (non-null) Video object here, yay! } else if (...) { // maybe there is the Audio here }
The above is actually just a stupid C # way to write a one-time pattern matching on an unlimited discriminatory union (an object is a union of every other type in C # :-)
user166390
source share