Question about using the var keyword

I want to have something like:

var somevar; if (cond) { var= something; // a lot of code } else var = somethingElse; 

However, the compiler yells that var must be initialized before using it that way. How to do it. or how to fulfill this situation?

+4
source share
10 answers

You can not. When using var, you must initialize the variable in the declaration ... otherwise the compiler does not know which type to make.

Variables defined with var are still statically typed ... the compiler simply introduces a type based on the assignment in the declaration. If you are looking for something dynamically typed, you can try the dynamic type if you are using .NET 4.0.

In your case, you need to specify the type in the declaration.

+8
source

As already mentioned, var is still a static type, it just means that the compiler displays this type at compile time, not runtime .

I think this is what you want:

 object somevar; if (cond) { somevar = something; // a lot of code } else somevar = somethingElse; 
+3
source

"var" is just a tool that allows the compiler to get the exact type. But for this you need to initialize it with an expression that returns this type.

Or do not use var , but use the type that, as you know, will be assigned by this expression later.

+2
source

If both condition values ​​are the same type, such as a string, do the following:

 var somevar = ""; 

To initialize it.

+2
source

You use the var keyword as a variable. replace var with somevar in the example.

+1
source

Do not use var in this situation. The way the compiler determines the type of a variable is by analyzing that on the right side. If you don’t give anything on the right side, the compiler will not be able to figure it out.

+1
source

When declaring a variable with var you must immediately assign a value to it, so the compiler can know its type.

Although you could make a compiler smart enough to find the first use, the C # compiler does not understand this code.

You can use:

 var someVar = cond ? someThing : someThingElse; if (cond) { // A lot of code } 

If someThing and someThingElse are of the same type. I think it is clearer to understand the possible values ​​of someVar, but you double check the cond.

+1
source

The following article details how to use var.

The var keyword tells the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The inferred type can be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library.

var can only be used when a local variable is declared and initialized in the same expression; a variable cannot be initialized to zero or a group of methods or an anonymous function.

Implicitly Typed Local Variables (C # Programming Guide)

0
source

You can use the object keyword, but you still need to know what type it has when unpacking, so var is a better idea if it is assigned when declaring, as mentioned earlier.

 object a = something; SomeCustomObject b = (SomeCustomObject)a; 
0
source

If "something" and "somethingElse" are of the same type, initialize somevar by default of that type.

 var somevar = default(TYPE GOES HERE); if (cond) { somevar = something; // a lot of code } else somevar = somethingElse; 
0
source

Source: https://habr.com/ru/post/1332513/


All Articles