Edward Tanguay
Edward Tanguay

Reputation: 193312

Is there a way to use var when variable is defined in an if/else statement?

I use var whenever I can since it's easier not to have to explicitly define the variable.

But when a variable is defined within an if or switch statement, I have to explicitly define it.

string message;
//var message; <--- gives error
if (error)
{
    message = "there was an error";
}
else
{
    message = "no error";
}

Console.WriteLine(message);

Is there a way to use var even if the variable is defined inside an if or switch construct?

Upvotes: 7

Views: 2212

Answers (3)

Baltasarq
Baltasarq

Reputation: 12212

In Python you could do exactly that:

if error:
    message = "there was an error";
else:
    message = "no error";

print(message);

Or even:

message = "there was an error" if error else "no error"

This is possible because Python does not require you to define the variable: the first time you assign a value to a variable, it is created, next times it is modified.

However, this is not the case of C#; as you yourself have confirmed, there are various caveats that do not allow you to code as in Python. First of all, you need to define each variable you are going to use (and its type, but keep reading), and secondly, there is scope. In C# a variable created inside an if branch will not exist further the end of that branch.

And well, then there is var. This is a special shortcut to the type of the value at the right of the assignment operator ("="). It does not mean "I will tell you the type of the variable later". It means "the type of the variable is the same of the expression I'm assigning it to.". In other words, you can only use var when you're assigning a value to a variable at the same time you are defining the variable.

Hope this helps.

Upvotes: 0

Evk
Evk

Reputation: 101483

You can't and you can confirm by looking at documentation:

The following restrictions apply to implicitly-typed variable declarations:

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.

So to use var you must always initialize it in the same statement according to the rules above.

Initializing it to some default value does not have the same semantics as unitialized variable. For example:

string message;
if (error) {
    message = "there was an error";
}
else {
    // forgot to assign here
}
// Compiler error, use of potentially uninitialized variable
Console.WriteLine(message);

But

var message = "";
if (error) {
    message = "there was an error";
}
else {
    // forgot to initialize
}
// all fine
Console.WriteLine(message);

So, just use string message;.

Upvotes: 5

Marc Gravell
Marc Gravell

Reputation: 1062800

No. You could use a conditional in this case, though:

var message = error ? "there was an error" : "no error";

But other than that: no. You'll need to specify the type, or use an initial explicit value. I advise against the latter as it removes the definite assignment check.

Upvotes: 8

Related Questions