I have a property which looks like this.
public int NumberOfElephants { get; set; }
this property is in an observablecollection and it has to notify another property that it has changed.
how would i do the following
public int NumberOfElephants { get; set { OnPropertyChanged("totalAnimals"); }
without the code needing to be like this
private int _numberOfElephants;
public int NumberOfElephants {
get {
return _numberOfElephants;
}
set {
_numberOfElephants = value;
OnPropertyChanged("totalAnimals");
}
}
You don’t. You can’t.
Automatically implemented propertieS only work when the property is trivial – when no code is needed for the get/set beyond “return the variable’s value” or “set the variable’s value”. You can make it shorter with reformatting, of course… I’d write that as:
Actually, I’d use “opening brace on a line on its own” for the start of the set and the start of the property, but I’ve kept your favoured style for those. But having “single expression
get/setimplementations” on a single line can make classes with lots of properties much cleaner.