Properties in C # in Unity (Unity5) - you can avoid variable lining

With Unity5 (it’s hard to know exactly which version of C # / Mono / .Net is used), we perform the following properties:

private int _distance; public int Distance { private set { _distance = value; controls.Blahblah(_distance); } get { Debug.Log("hah!); return _distance; } } 

But consider new “automatic properties” in C # that seem similar to

  public int Distance {get; set;} // ? 

but i don't know how to "do something" in getter / setter?

Or, in other words, is there a way to automatically generate a backup variable (as well as the convenience of keeping it private) when it manually creates a property?

Repeat, since this has been marked as a duplicate, how can I "do things" in the automatic identifier Property during getter / setter ...

... or vice versa...

how to hide, get rid of or automatically provide support if you write your own "manual" properties?

Note that, of course, you or another programmer may accidentally touch the _underscore support variable: is there any way to avoid this.

+4
source share
5 answers

You cannot use automatic properties if you want to "do something" in getter / setter, except to simply assign this field. For the use case described in your code example, automatic properties are not an option. There is nothing wrong with having an explicit support field.

+3
source

If you have only a private variable without any other logic, you can use the 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;} public int Distance {get; set;}

But if you perform other actions, such as logging, then you should write it in the traditional way.

Edit

All his coding practice. I usually include private variables in #region p ...#endregion . And use only Properties to set - Distance and never used _distance. It is more a coding practice than an actual correction of what you are doing.

Another reason I am doing this: in WPF, we will need to raise the NotifyPropertyChanged event whenever we set the properties. It will be a mistake if I do not use the name of the property. Thus, this habit of using properties over private variables is stuck.

You cannot make a variable inaccessible for reading, just grouping it for readability and yes, this is a common human practice.

+3
source

Automatic properties do not allow you to "nothing" in them. They are simply abbreviated for:

 public int Distance { get { return _distance; } set { _distance = value; } } private int _distance = default(int); 

There is nothing wrong with using a real variable in this property; it will not affect anything.

Keep in mind that if you want to edit a variable in an editor, you cannot use a property. You will need to open the public field:

 public int Distance; 
+2
source

If your get and set must have additional code, for example, for logging, you should implement them the way you did it first, if you just need to get and set the value, then automatically implemented properties are better, I mean 2- th approach.

+1
source

I know this is not the answer to your question. But you should not use properties in game programming. Properties look like parameters, but they are called as a method. Each call will slow down your game. They are not recommended to use properties in the official unity video.

But this is your decision.

0
source

Source: https://habr.com/ru/post/1242145/


All Articles