I wanted to use something like this:
if(x==5)
{
var mydb= ........ ;
}
else
{
var mydb = ........ ;
}
but it didn't work because I can't declare a variable inside if statement.
So I tried to do this:
var mydb;
if (x==5)
{
mydb= ............. ;
}
else
{
mydb=.............;
}
but id didn't work either because I had to initialize the variable (mydb).
So the question is: I don't necessarily know the type of the variable, can I declare it anyway and then change the type inside the if statement?
C# is statically typed unless you're running 4.0 with the dynamic
specifier, so changing the type is classically impossible except via polymorphism.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With