how can i convert from:
object[] myArray
to
Foo[] myCastArray
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To filter elements by
Footype:To try to cast each element to
Footype:Side note: Interestingly enough, C# supports a feature (misfeature?) called array covariance. It means if
Derivedis a reference that can be converted toBase, you can implicitly castDerived[]toBase[](which might be unsafe in some circumstances, see below). This is true only for arrays and notList<T>or other stuff. The opposite (array contravariance) is not true, that is, you cannot castobject[]tostring[].C# version 4.0 is going to support safe covariance and contravariance for generics too.
Example where array covariance might cause problems:
I speculate C# has array covariance because Java had it. It really doesn’t fit well in the overall C# style of doing things.