问题
With Unity5 (it's hard to know exactly what version of c#/Mono/.Net is being used), we do properties exactly like this:
private int _distance;
public int Distance
{
private set
{
_distance = value;
controls.Blahblah(_distance);
}
get
{
Debug.Log("hah!);
return _distance;
}
}
But consider the new "automatic properties" in c#, which seem to be like
public int Distance {get; set;} // ?
but I don't know how to "do something" in the getter/setter ??
Or in other words, is there a way to auto generate the backing variable (as well as the convenience -- to keep it private) when "manually" making a Property?
To repeat since this was marked as a duplicate, how can I "do stuff" in the automatic Property idiom during the getter/setter ...
... or conversely ...
how to hide, get rid of, or automatically supply the backer if you write your own "manual" properties?
Note that of course you or another programmer can accidentally touch the _underscore backing variable: is there any way at all to avoid that??
回答1:
You can't use auto-properties if you want to "do something" in the getter/setter, apart from just assigning to the field. For the use-case described by your example code, auto-properties are not an option. There is nothing wrong with having an explicit backing field.
回答2:
If you have only the private variable without any other logic then you can use Auto properties.
Class Something something
{
public int Distance
{
private set
{
_distance = value;
}
get
{
return _distance;
}
}
// Keep this at the end of the class
// In visual studio you can collapse region and wont attract
// attention/distracting in your editor.
#region data members
private int _distance;
#endregion data members
}
you can replace it with public int Distance {get; set;}
But if you do other actions like logging, then you have to write it in traditional way.
Edit
Its all coding practice. I generally enclose the private variables in a #region p ...#endregion. And use only the Properties for setting - Distance and have never used _distance. Its more of a coding practice than an actual fix for what you are doing.
One more reason I do that is - In WPF we would need to call NotifyPropertyChanged event whenever we set the properties. It will be a bug if I don't use the property name. So this habit of using Properties over private variables stuck.
You cannot make the variable un-discoverable, just grouping them together for easy readability and Yes this is human enforced practice.
回答3:
Automatic properties do not allow you to do 'anything' in them. They are simply shorthand for:
public int Distance
{
get
{
return _distance;
}
set
{
_distance = value;
}
}
private int _distance = default(int);
There is nothing wrong with using an actual variable in this property, it will not affect anything.
One thing to keep in mind though, is if you want to edit a variable in the editor, you cannot use a property. You will need to expose a public field:
public int Distance;
回答4:
If your get and set needs to have extra code like you have for logging, you have to implement them the way you did it first way, if you just need to get and set the value then auto implemented properties are better i mean the 2nd approach.
来源:https://stackoverflow.com/questions/35580895/properties-in-c-sharp-in-unity-unity5-can-you-avoid-the-backing-variable