Question

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 ?

Was it helpful?

Solution

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.

OTHER TIPS

Its just that instead of creating a variable, you created a property on which in future you want some custom work while setting and retrieving value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top