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 ?

有帮助吗?

解决方案

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top