Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert C# 6.0 default propertyvalues to C# 5.0

public List<string> MembershipIds
{
    get;
    set;
} = new List<string>();

I got Invalid token

"=" in class, struct or interface member declaration.

This is C# 6 feature. How to convert it to C# 5?

like image 849
mbrc Avatar asked Dec 27 '15 18:12

mbrc


People also ask

What is C equal to in Fahrenheit?

Convert celsius to fahrenheit 1 Celsius is equal to 33.8 Fahrenheit.

What does C mean in conversion?

Quick Celsius (°C) / Fahrenheit (°F) Conversion:°C.

What is the fastest way to convert Celsius to Fahrenheit?

If you find yourself needing to quickly convert Celsius to Fahrenheit, here is a simple trick you can use: multiply the temperature in degrees Celsius by 2, and then add 30 to get the (estimated) temperature in degrees Fahrenheit.

What temperature in Fahrenheit is 50 C?

Answer: 50° Celsius is equal to 122° Fahrenheit.


1 Answers

There is no easy way to do it while leaving the auto-property in place.

If you do not require an auto property, convert the code to using a private variable and a non-automatic property:

private List<string> membershipIds = new List<string>();
public List<string> MembershipIds {
    get { return membershipIds; }
    set { membershipIds = value; }
}

If you do require the auto-property, you need to make the assignment in the constructor:

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
}
like image 82
Sergey Kalinichenko Avatar answered Oct 02 '22 03:10

Sergey Kalinichenko