Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are automatic properties for private variables unnecessary?

I was look at somebody else's code and came across this piece of code

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

My thinking is that those automatic properties for private variables are unnecessary. Am I right in thinking so ?

like image 720
devcoder Avatar asked Feb 21 '23 01:02

devcoder


1 Answers

My thinking is that those automatic properties for private variables are unnecessary. Am I right in thinking so ?

They aren't necessary, but they also don't really hurt anything. That being said, they don't really help anything either, as they're purely an implementation detail, so switching from a field to a property later wouldn't be a breaking change.

The only real reason to potentially do this would be if you knew, in the future, you would want custom logic on get or set, and you were using something that required the syntax to be different for properties, such as reflection. In this case, making them auto-properties now would prevent any need for changing the code later.

like image 117
Reed Copsey Avatar answered Mar 03 '23 13:03

Reed Copsey