문제

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