开发者

How to change a variable type in C#?

开发者 https://www.devze.com 2022-12-25 10:18 出处:网络
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.

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:

va开发者_如何学编程r 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?


No, you can't. Variables never change their types. What types are you actually interested in? Could you declare the variable to be some common base type or an interface that both of them implement? If you can tell us more about your situation, we may be able to help you more.

C# is a statically typed language (leaving aside C# 4; that introduces dynamic typing where you really need it, but it's worth understanding the "normal" C# way of doing things first). The compiler needs to know the type of the variable so that if can work out 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 check that the type has a Length property and emit a call to it in the IL.


you can use:

object mydb = null;

if(x==5)
{
    mydb= ........ ;
}
else 
{
    mydb = ........ ;
}

but you have to unbox or cast the object back to its proper type when you want to access the object's fields,properties,methods. unless you will wait for C# 4 which can facilitate dynamic method (exact terminology: dynamic dispatch?) invocation


C# is statically typed unless you're running 4.0 with the dynamic specifier, so changing the type is classically impossible except via polymorphism.


You can declare base type and inherit both types from it. But the main question is: How you gonna use it if you don't know its type?


I assume that you have two incompatible types from two different libraries that both represent a database. You could write an interface with the common operations and write adapters that wrap the classes coming from the libraries and implement you interface. Than the type of your variable mydb could be that interface.

Of course you could use objectas type for mydb and use dynamic type tests and casts, but that would be a very bad design decision in this case.


YOU CAN USE SYSTEM.CONVERT CLASS LIKE THIS OR USE DYNAMIC

string possibleInt = "1234"; 
int count = Convert.ToInt32(possibleInt);

OR USE Explicit Conversions

In C#, you can use a cast operator to perform explicit conversions. A cast specifies the type to convert to, in round brackets. The syntax for performing an explicit conversion is shown in the following code example.

DataType variableName1 = (castDataType) variableName2;


You can use like this bool result = (condition) ? true : false ;

var myDB = (x==5) ? true : false ;
or
var myDB = x==5 ? "doing something when case is true" : "doing something when case is false" ;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号