更新时间:2022-12-23 12:18:33
您有一个声明(例如,if 或 while),就在您发布的代码之前, 没有花括号.
You have a statement (if or while, for example), right before the code you posted, without curly braces.
例如:
if (somethingIsTrue)
{
var user= new ApplicationUser {
UserName = model.myUser.Email,
Email = model.myUser.Email ,
};
}
是正确的,但是下面的代码:
is correct, but the code below:
if (somethingIsTrue)
var user = new ApplicationUser {
UserName = model.myUser.Email,
Email = model.myUser.Email ,
};
将导致 CS1023:嵌入的语句不能是声明或标记的语句.
will result in CS1023: Embedded statement cannot be a declaration or labeled statement.
根据@codefrenzy,原因是新声明的变量将立即超出范围,除非它包含在块语句中,可以从中访问它.
The reason, according to @codefrenzy, is that the newly declared variable will immediately go out of scope, unless it is enclosed in a block statement, where it can be accessed from.
不过在以下情况下编译会通过.
The compilation will pass in the following cases though.
如果你只初始化一个类型的新实例,而不声明一个新变量:
If you only initialize a new instance of a type, without declaring a new variable:
if (somethingIsTrue)
new ApplicationUser {
UserName = model.myUser.Email,
Email = model.myUser.Email ,
};
或者如果您为现有变量赋值:
or if you assign a value to an existing variable:
ApplicationUser user;
if (somethingIsTrue)
user = new ApplicationUser {
UserName = model.myUser.Email,
Email = model.myUser.Email ,
};