Why is C # automatically implementing properties?

In all the examples I see, the automatic C # properties are made public, even in the MSDN documentation examples. Based on the background in C ++, I was always taught that it is a good idea to make element data private if there is no good reason not to.

Why the following were never used (at least I never saw it):

private Name { get; set; } 

I looked at the MSDN documentation and read a few guides on automatically implemented properties, but there seems to be no advice on their pros and cons, and when to avoid them. Do automatic properties compromise program security? Are there any situations where they should be avoided? In what situations are they the perfect choice?

Thanks.

+6
source share
6 answers

You are right that automatically implemented properties that simply expose a background field do not matter much in a public field.

As Alan Kay said :

But most people using setters simply use them to simulate direct assignments to internal variables, and this violates the spirit and intent of a real OOP .

However, there is an advantage for an automatically implemented property over a public field, and this is because it is an unconditional change for a subsequent review of the implementation. If you have a public field, and code outside your class processes this public field, you cannot change it to a personal field in a future version of this class, otherwise any other code that concerns this field must be recompiled. In contrast, if you have a public property, you can revise the implementation of this property in a future version, and client classes can continue to use it with zero changes.

Therefore, it is useful to use automatically implemented properties for properties that the trivial getter and setter implementations will have right now , but in the future this may have more complex implementations.

+6
source

You asked yourself why you were always taught that it is a good idea to make members private?

This is because (among other reasons) it is detail . A detail of "storing data in memory", and this is an unimportant detail for any object that wants to receive or install data. Other classes do not need to worry about whether he can somewhere access a certain memory slot - he just needs an interface for which he can pass or receive a value - there are getters and setters or properties.

By separating the property from the "memory-based memory" part, we have many advantages. First of all, we can redefine the behavior of getting and setting without violating any code that uses the property. We can also use the property as an abstraction to retrieve data for several different implementations. This becomes extremely useful for testing / bullying behavior and providing alternative storage. If other classes depend on the implementation details of "memory", you cannot change the behavior of your class without breaking it.

Before the auto properties appeared, we will usually store the field and create a getter and setter for encapsulation for the reasons described above. An automatic property automates this for us. We could write code that usually uses fields everywhere in the code, but we do it with the idea "I will do this as a field for now, but it can be changed later if the criteria changes."

Since the class knows about this own implementation, it is usually a senseless desire to create private properties of a car, you do not hide already known details. Protected auto properties can be useful if you need to subclass.

As for situations in which they should be avoided: when you want read-only data. (data that will not be changed after the creation of the object). Auto properties lack syntax to create an automatic property supported by read-only data.

+4
source

Authorized properties have a private support element. The compiler adds them for you. Its just a shortcut to

 private int _aMember; public int AMember { get {return _aMember;} set {_aMember = value;} } 

You use them, you have no real logic in getter / setter (except for the need for encapsulation).

+2
source

Auto-updated properties are just a shortcut to a common template. Every time you have a private member with the corresponding get and set functions in C ++, you can do the same with the auto attribute in C #. They do not introduce any other best practices or security concerns.

+2
source

Authorized properties with public getters and setters are shortcuts to private support fields with trivial get and set implementations.

You should think of them as being similar to private fields with the public C ++ get and set methods. This is the role of properties in C #.

+2
source

Properties are not "composite" data. They are yours

 const T& field() const; T& field(); 

or methods for obtaining and installing, i.e. they are accessories.

If you do not need a member element, do not express it in the properties. In the case of auto-generated, you can simply (with C # 2.0 afaik) write

 int SomeProperty { get; private set; } 
+1
source

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


All Articles