Reputation: 839
I want to have something like:
var somevar;
if (cond)
{
var= something;
// a lot of code
}
else var = somethingElse;
However the compiler screams that a var should be initialized before using it in this way. How to do it. or how to accomplish this situation?
Upvotes: 4
Views: 300
Reputation: 2268
You use the keyword var as a variable. replace var with somevar in the example.
Upvotes: 1
Reputation: 489
If 'something' and 'somethingElse' are of the same type, initialize somevar to the default of that type.
var somevar = default(TYPE GOES HERE);
if (cond)
{
somevar = something;
// a lot of code
}
else somevar = somethingElse;
Upvotes: 0
Reputation: 2655
You can use the object keyword but you still need to know what type it is when unboxing it so the var is a better idea if assigned to when declaring as said previously.
object a = something;
SomeCustomObject b = (SomeCustomObject)a;
Upvotes: 0
Reputation: 245429
You can't. When using var, you have to initialize the variable in the declaration...otherwise there is no way the compiler knows what type to make it.
Variables defined using var
are still statically typed...the compiler just infers the type based on the assignment in the declaration. If you're looking for something that is dynamically typed, you could try the dynamic
type if you're using .NET 4.0.
In your case, you need to specify the type at declaration.
Upvotes: 8
Reputation: 6255
The article below gives great detail into how var can be used.
The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The inferred type may 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 statement; the variable cannot be initialized to null, or to a method group or an anonymous function.
Implicitly Typed Local Variables (C# Programming Guide)
Upvotes: 0
Reputation: 19842
As other's have mentioned, var
is still a static type, it just means that the compiler infers that 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;
Upvotes: 3
Reputation: 64487
If both values of the condition are the same type, say string, do something like this:
var somevar = "";
To initialise it.
Upvotes: 2
Reputation: 52518
When declaring a variable using var
, you must immediatly assign a value to it, so the compiler can know its type.
Although you could make a compiler that is smart enough to find the first usage, the C# compiler does not understand this code.
You could use:
var someVar = cond ? someThing : someThingElse;
if (cond) {
// A lot of code
}
If someThing
and someThingElse
are of the same type. I think this is clearer to understand the possible values of someVar, but you test for cond twice.
Upvotes: 1
Reputation: 103740
Don't use var in that situation. The way the compiler figures out what type your variable is, is by analyzing what's on the right hand side. If you're not giving it anything on the right side, there's no way for the compiler to figure it out.
Upvotes: 1
Reputation: 39284
"var" is just a means to instruct the compiler to derive the exact type. But for that to work, you need to initialize it with an expression that returns that type.
Or don't use var
but use the type you know that both expression will assign later.
Upvotes: 2