嵌入式语句不能是声明或带标签的语句 [英] Embedded statement cannot be a declaration or labeled statement
问题描述
我正在尝试使用声明身份asp.net创建一个用户 创建声明身份用户时出现此错误.
I am trying to create a user using claim identity asp.net I get this error while creating claims identity user.
ApplicationUser user = new ApplicationUser {
EmailConfirmed = true,
UserName = model.myUser.Email,
Email = model.myUser.Email ,
PhoneNumber = model.myUser.PhoneNumber,
PhoneNumberConfirmed = true,
UserImagePath = model.myUser.UserImagePath,
FirstName= model.myUser.FirstName,
LastName = model.myUser.LastName,
DateOfBirth = model.myUser.DateOfBirth,
Culture = model.myUser.Culture,
Role = model.myUser.Role
};
但是代码是
var user= new ApplicationUser {
UserName = model.myUser.Email,
Email = model.myUser.Email ,
};
它工作得很好,所以我想知道哪里出了问题
it worked perfectly, so i want to know what is wrong
推荐答案
在发布代码之前,您有一条声明(例如, 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所述,原因是新声明的变量将立即超出范围,除非将其包含在可以从中访问它的block语句中.
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 ,
};
这篇关于嵌入式语句不能是声明或带标签的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!