When should you use c # property Property

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/ )

  1. It is a class attribute logic.
  2. Properties should be relatively stable, should not change themselves. The authors noted DateTime.Nowa violation of the two.
  3. Properties spent time reading and writing and reading and writing should be member variables of the same, can not exceed 10 times.
  4. 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.
  5. You should not change the state of the object attribute of a read operation. (Except the one above)
  6. Attribute read operation should never succeed, not throw an exception.
  7. 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!
  8. Do not let the property read as an array or list.
  9. 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.

Guess you like

Origin www.cnblogs.com/-cxy/p/12579379.html