嵌入语句不能是声明或标记语句 [英] Embedded statement cannot be a declaration or labeled statement

查看:81
本文介绍了嵌入语句不能是声明或标记语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用声明身份 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

推荐答案

您有一个声明(例如,ifwhile),就在您发布的代码之前, 没有花括号.

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 ,
   };

这篇关于嵌入语句不能是声明或标记语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆