This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

Join us in building a kind, collaborative learning community via our updated
Code of Conduct.

3 Answers
3

In terms of the refactoring, it's just syntactic sugar that allows you to use expressions to define certain things (including getters and/or setters) with the important distinction that it supports a single expression only. The goal is to make the code more concise and it's part of the enhancements introduced in C# 6.

The property-initialisation syntax is a part of the C# language so is not dependent on the .NET Framework version. The version of Visual Studio you are using does indeed impact on the C# versions available to you, but note that is possible, for example, to use what is at the time of writing the newest C# version - 7.x - with the not-current Visual Studio 2015.

...but no. private string member1; is not referenced by the Member1 property. Indeed in this example, member1 is not referenced at all and may well be optimized away by the compiler! What you really wrote was:

public class MyClass
{
public string Member1 { get; set; }
}

The above is an auto-implemented property, as introduced in C# 3.0. Note that the get and set keywords are not followed by bodies.

In C# 3.0 and later, auto-implemented properties make
property-declaration more concise when no additional logic is required
in the property accessors... When you declare a property [this way], the compiler creates a private, anonymous backing field that
can only be accessed through the property's get and set accessors.

So, just like in our "traditional, bog-standard field-backed property" example we get a field-backed property, except this time we don't define the field in our code and it is anonymous to us. In the compiler we trust!

get => member1 - this is a property get accessor with an expression body definition, as introduced in C# 6. It is just syntactic sugar equivalent to get { return member1; }.

set => member1 = value; is a property set accessor with an expression body definition. This syntax was introduced for setters in C# 7. It is syntactic sugar for set { member1 = value; }.

To end this summary, C# 6 also introduced initializers for auto-properties. An initializer directly initializes the backing field. Consider this elegant lightweight code to declare and initialize a simple, (hidden, anonymous) field-backed readable and writable property: