No, you can’t. Variables never change their types. What types are you interested in? Could you declare the variable as some common base type or interface that they both implement? If you can tell us more about your situation, we can help you more.
C # is a statically typed language (leaving C # 4 aside, which introduces dynamic typing where you really need it, but you should understand the “normal” way of C # to do something in the first place). The compiler must know the type of the variable so that if it could decide what each reference to it means. For example, if you use
string x = "text"; int y = x.Length;
the compiler needs to know that x is of type string so that it can verify that the type has the Length property and emits its call in IL.
Jon skeet
source share