在保留自动财产的同时,没有简单的方法可以做到这一点.
如果您不需要自动属性,请将代码转换为使用私有变量和非自动属性:
private ListmembershipIds = new List (); public List MembershipIds { get { return membershipIds; } set { membershipIds = value; } }
如果确实需要auto-property,则需要在构造函数中进行赋值:
public ListMembershipIds { 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 (); } // 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 }