Blog Stats
  • Posts - 24
  • Articles - 0
  • Comments - 39
  • Trackbacks - 93

 

C#: Properties versus getter methods

You might be wondering why some classes in the .NET Framework have properties, other classes have GetXXX methods, and some have a combination of the two.  The reason is that the semantic, or meaning, is quite different.

Consider this class:

public class Customer
{
 public int CustomerID;
 public string FirstName;
 public string LastName;
 
 public Order[] GetOrders()
 {
  // do database work
 }
}

The CustomerID, FirstName, and LastName represent the customer state.  The orders are derived from the customer state using GetOrders().  In fact I do not support having method like this because it presumes that a canonical representation of Order exists.  That's another topic. 

A great rule-of-thumb regarding properties is to consider that the Visual Studio debugger executes the getter method when watching an object.  That is, property accessors are executed at unpredictable times and should thus not cause any discernable side-effects.  Abusing properties can lead to heisenbugs.

Another reason to use a method is that order retrieval is likely to be parameterized.

Finally, a good practice is for property access to be computationally cheap; client code should not be forced to place the property value into a local variable - it's premature optimization.  Expensive code should be placed in methods - client code is sure to store and reuse the result.  The exception is if the property getter method caches the result in a private field, which can be a challenge as the class instance is mutated.

The property syntax in .NET languages is not syntactic sugar.  It adds considerable richness to classes.


Feedback

# re: C#: Properties versus getter methods

Gravatar Some clarifications...

http://blogs.msdn.com/kaevans/archive/2004/05/02/124778.aspx 5/2/2004 1:00 PM | Kirk Allen Evans

# re: C#: Properties versus getter methods

Gravatar I was thinking about Java the whole time I was writing this. I worked on an EJB project where we wanted to expose a class like Customer over SOAP. It had various properties that we wanted to expose, and convenience methods for getting related information. Of course, those properties and methods were indistinguishable from the point of view of the SOAP stack. The foremost problem was that there were cycles in the object graph, making serialization difficult.

Another problem is that the convenience data was read-only, whereas the state data was read-write. Therefore it was clumsy to use the entire Customer object in a UpdateCustomer method.

My point was really that C# is more expressive than Java. 5/3/2004 8:41 AM | Eron Wright

# re: C#: Properties versus getter methods

Gravatar another "problem" with computationally expensive getters is that they may be triggered in the debugger if you watch the members of the object ... this can be handy sometimes, but dangerous and/or weird depending on what the getter actually does
11/19/2004 1:14 PM | Brad

# re: C#: Properties versus getter methods

Gravatar Brad, you make a good point that I keep forgetting to mention myself. Thanks.


Me, the class I showed above is indeed using fields, not properties, for simplicity. Properties do have accessor methods so that encapsulation is achieved. 4/12/2005 10:03 AM | Eron Wright

# re: C#: Properties versus getter methods

Gravatar Small checklist - http://dotnettipoftheday.org/tips/property_vs_method.aspx 11/23/2007 10:45 AM | Kostya

Post a comment





 

 

 

Copyright © Eron Wright