Eric Lippert recommendations about what to see when to use C # attributes of the When the I Should the Write A Property? . (From http://fresky.github.io/2015/04/02/when-use-property-in-csharp/ )
- It is a class attribute logic.
- Properties should be relatively stable, should not change themselves. The authors noted
DateTime.Now
a violation of the two. - Properties spent time reading and writing and reading and writing should be member variables of the same, can not exceed 10 times.
- A common pattern is used to cache a property takes much time delay value may be calculated. But be careful, in particular, more needs to be done to be careful when thread-safe. Think of a singleton how to do it multi-thread safe.
- You should not change the state of the object attribute of a read operation. (Except the one above)
- Attribute read operation should never succeed, not throw an exception.
- When the debugger debugger, if we look at an object with a debugger state, the default will call this property a read operation, so if you read will change the state of the object, it indicates that the debugger will change the program status!
- Do not let the property read as an array or list.
- Do not return the read operation of a variable property value type. Because the value of the type will be a copy, you do not actually change the original value.