How to change a vairable type in C#?

Posted by Mosho Mulan on Stack Overflow See other posts from Stack Overflow or by Mosho Mulan
Published on 2010-04-05T06:43:14Z Indexed on 2010/04/05 6:53 UTC
Read the original article Hit count: 163

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about variable