mbr*_*brc 7 .net c# properties c#-6.0
public List<string> MembershipIds
{
get;
set;
} = new List<string>();
Run Code Online (Sandbox Code Playgroud)
我得到了无效令牌
类,结构或接口成员声明中的"=".
这是C#6功能.如何将其转换为C#5?
das*_*ght 13
在保留自动财产的同时,没有简单的方法可以做到这一点.
如果您不需要自动属性,请将代码转换为使用私有变量和非自动属性:
private List<string> membershipIds = new List<string>();
public List<string> MembershipIds {
get { return membershipIds; }
set { membershipIds = value; }
}
Run Code Online (Sandbox Code Playgroud)
如果确实需要auto-property,则需要在构造函数中进行赋值:
public List<string> MembershipIds { get;set; }
...
// This constructor will do the assignment.
// If you do not plan to publish no-argument constructor,
// it's OK to make it private.
public MyClass() {
MembershipIds = new List<string>();
}
// All other constructors will call the no-arg constructor
public MyClass(int arg) : this() {// Call the no-arg constructor
.. // do other things
}
public MyClass(string arg) : this() {// Call the no-arg constructor
.. // do other things
}
Run Code Online (Sandbox Code Playgroud)