Are automatic properties needed for private variables?

I looked at another user's code and came across this piece of code

private string _deviceName { get; set; } private string _deviceAlias { get; set; } 

I believe that these automatic properties for private variables are not needed. Do I think so?

+4
source share
2 answers

I believe that these automatic properties for private variables are not needed. Do I think so?

They are not needed, but they also do not hurt anything. At the same time, they also do not help anything, since they are purely implementation details, so moving from a field to a property later will not be a violation of the changes.

The only real reason to potentially do this would be if you knew that in the future you would need custom logic for get or set, and you used something that required the syntax to be different for properties like reflection. In this case, creating automatic properties will now prevent the need to change the code later.

+8
source

Simply, instead of creating a variable, you created a property in which in the future you will need some kind of user work when setting up and retrieving the value.

0
source

All Articles