How to change variable type in C #?

I wanted to use something like this:

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

but this did not work, because I cannot declare a variable inside the 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 do not necessarily know the type of the variable, can I declare it anyway, and then change the type inside the if statement?

+6
variables c # types
source share
7 answers

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.

+11
source share

you can use:

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

but you need to unpack or return the object to the appropriate type if you want to access the fields of the object, properties, methods. unless you wait for C # 4, which can be facilitated by a dynamic method (exact terminology: dynamic dispatch?) invocation

+6
source share

C # is statically typed unless you use 4.0 with a dynamic specifier , so changing the type is classically impossible, except through polymorphism.

+3
source share

You can declare a base type and inherit both types from it. But the main question: How are you going to use it if you do not know its type?

+1
source share

I assume that you have two incompatible types from two different libraries, which are a database. You can write an interface with common operations and write adapters that transfer classes from libraries and implement the interface. What type of your variable mydb can be this interface.

Of course, you can use object as a type for mydb and use dynamic type and cast tests, but in this case it will be a very bad design decision.

+1
source share

YOU CAN USE THE SYSTEM CLASS. ENVELOPE HOW TO OR USE SPEAKERS

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

OR USE Explicit conversions

In C #, you can use the translation operator to perform explicit conversions. The listing defines the type of conversion to parentheses. The syntax for performing an explicit conversion is shown in the following code example.

 DataType variableName1 = (castDataType) variableName2; 
0
source share

Can you use 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
source share

All Articles