User Tools

Site Tools


notes:csharp:properties

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
notes:csharp:properties [2020/06/16]
leszek
notes:csharp:properties [2020/06/16] (current)
leszek
Line 14: Line 14:
   * Properties and data members are not binary compatible. It means that you have to recompile all code that uses the public data member when you change it to a public property.   * Properties and data members are not binary compatible. It means that you have to recompile all code that uses the public data member when you change it to a public property.
   * Property accessors should not perform lengthy computations or perform database queries.   * Property accessors should not perform lengthy computations or perform database queries.
-  * Consider cashing a value that you retrieve in the getter. Alternatively,​ use the ''​%%Lazy<​T>​%%''​ class. 
  
 **Indexers** **Indexers**
Line 21: Line 20:
   * Single-dimension indexers with numeric parameters can participate in data binding.   * Single-dimension indexers with numeric parameters can participate in data binding.
   * You can't create implicit indexers as you can with properties.   * You can't create implicit indexers as you can with properties.
 +
 +**Caching**
 +
 +  * When the getter is called occasionally,​ you can use the ''​%%Lazy<​T>​%%''​ class to retrieve the value only when needed.
 +  * When the getter is used often and when it's valid to cache the value, you can load it in the ctor and use the cached value as the property return value.
 +  * In all other cases when the property does not behave like a property (e.g., calls a database in the getter), use methods to manage cached values.
  
 A virtual property: A virtual property:
Line 67: Line 72:
 </​code>​ </​code>​
  
-A property with lazy instantiation ​in the getter:+A property with lazy call to a database ​in the getter. We avoid the work of retrieving the value when it's not needed. On the other hand, the first call to the property has to call the database:
 <code csharp> <code csharp>
 public class Something public class Something
notes/csharp/properties.txt ยท Last modified: 2020/06/16 by leszek