Why can't we define a variable inside an if st

2019-01-13 17:39发布

Maybe this question has been answered before, but the word if occurs so often it's hard to find it.

The example doesn't make sense (the expression is always true), but it illustrates my question.

Why is this code valid:

StringBuilder sb;
if ((sb = new StringBuilder("test")) != null) {
    Console.WriteLine(sb);
}

But this code isn't:

if ((StringBuilder sb = new StringBuilder("test")) != null) {
    Console.WriteLine(sb);
}

I found a similar question regarding a while statement. The accepted answer there says that in a while statement, it would mean the variable would be defined in each loop. But for my if statement example, that isn't the case.

So what's the reason we are not allowed to do this?

5条回答
Viruses.
2楼-- · 2019-01-13 17:59

This is because section 8.5.1 of the C# language spec. states:

Furthermore, a variable initializer in a local variable declaration corresponds exactly to an assignment statement that is inserted immediately after the declaration.

This basically means that, when you do:

StringBuilder sb = new StringBuilder("test")

You're, in effect, doing the exact same thing as:

StringBuilder sb; sb = new StringBuilder("test")

As such, there is no longer a return value for your check against != null, as the assignment isn't a single expression, but rather a statement, which is a local-variable-declarator comprised of an identifier followed by an expression.

The language specification gives this example, stating that this:

void F() {
   int x = 1, y, z = x * 2;
}

Is exactly equivalent to:

void F() {
   int x; x = 1;
   int y;
   int z; z = x * 2;
}
查看更多
放我归山
3楼-- · 2019-01-13 18:02

As in Jeff's comment, the output is null for this code:

StringBuilder sb = new StringBuilder("test");

and the if statement requires a value.

查看更多
forever°为你锁心
4楼-- · 2019-01-13 18:05

Try C#7's Pattern Matching.

Using your example:

if (new StringBuilder("test") is var sb && sb != null) {
    Console.WriteLine(sb);
}
查看更多
时光不老,我们不散
5楼-- · 2019-01-13 18:11

This has to do with the difference between a statement, and an expression. An expression has a value, whereas a statement does not.

Using your examples, notice these classifications:

StringBuilder sb; // statement

sb = new StringBuilder("test") // expression

StringBuilder sb = new StringBuilder("test"); // statement

Notice that only the middle portion is a expression.

Now we move onto your conditional statement. The syntax for using the not-equals operator is

expression != expression

So on both sides of the != you need something that actually has a value (this just makes sense). Ergo, you cannot have statements on either side of the operator. This is why the one version of your code works, while the other does not.

查看更多
萌系小妹纸
6楼-- · 2019-01-13 18:16

Instead of:

if ((StringBuilder sb = new StringBuilder("test")) != null) {
    Console.WriteLine(sb);
}

One could also write:

for (StringBuilder sb = new StringBuilder("test"); sb != null; sb = null) {
    Console.WriteLine(sb);
}

This for loop will execute once if your variable it not null. At the end of the loop, your temporary variable is set to null. The loop condition then evaluates to false, and the next statement continues after the closing brace is executed. Exactly as your if statement originally intended.

查看更多
登录 后发表回答