1

in the last C# versions of the properties get and set with more sentences the way to write is:

ObservableCollection<Product> products;
public ObservableCollection<Product> Products
{
   get
   {
     return products;
   }
   set
   {
     products = value;
     OnPropertyChanged("Products");
   }
}

But how is that in C# 6.0? because the new style is with lambda operator:

ObservableCollection<Product> products;
public ObservableCollection<Product> Products
{ 
  get => products; 
  set => products= value; 
}

Thanks.

Ejrr1085
  • 975
  • 2
  • 16
  • 29

1 Answers1

1

The Auto-Implemented Properties (auto-properties) have existed since c# 3.

public int Age { get; set; }

C# 6 do some improvements, one of them was the Auto-Property Initializers.

public int Age { get; } = 30;

But I think what you want to use is another feature, the Expression-bodied function members

public void SayHello(string name) => Console.WriteLine("hello {0}", name);

public string FullName => string.Format("{0} {1}", FirstName, LastName);

Expression-bodied function members
The body of a lot of members that we write consist of only one statement that can be represented as an expression. You can reduce that syntax by writing an expression-bodied member instead. It works for methods and read-only properties.

Expression-bodied function members

You can't use use them in your case, because your property is not read-only.

Maybe this can help you.
How to implement INotifyPropertyChanged in C# 6.0?

UPDATE:

C# 7 introduces the syntax you want, but only for single-line expressions.

Property set statements(link)
If you choose to implement a property set accessor yourself, you can use an expression body definition for a single-line expression that assigns a value to the field that backs the property.

public class Location
{
   private string locationName;

   public Location(string name) => Name = name;

   public string Name
   {
      get => locationName;
      set => locationName = value;
   } 
}
Pedro Perez
  • 842
  • 6
  • 23