Geeks With Blogs
Eron Wright - All Killer No Filler blog

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.

Posted on Sunday, May 2, 2004 8:18 AM Technology | Back to top


Comments on this post: C#: Properties versus getter methods

# re: C#: Properties versus getter methods
Requesting Gravatar...
Left by Kirk Allen Evans on May 02, 2004 1:00 PM

# re: C#: Properties versus getter methods
Requesting 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.
Left by Eron Wright on May 03, 2004 8:41 AM

# re: C#: Properties versus getter methods
Requesting 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
Left by Brad on Nov 19, 2004 1:14 PM

# re: C#: Properties versus getter methods
Requesting 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.
Left by Eron Wright on Apr 12, 2005 10:03 AM

# re: C#: Properties versus getter methods
Requesting Gravatar...
Small checklist - http://dotnettipoftheday.org/tips/property_vs_method.aspx
Left by Kostya on Nov 23, 2007 10:45 AM

# re: C#: Properties versus getter methods
Requesting Gravatar...
I enjoy reading the report, too. It′s easy to understand that a journey like this is the biggest event in ones life.
Left by chi flat iron on Apr 01, 2011 2:11 AM

# re: C#: Properties versus getter methods
Requesting Gravatar...

I must admit this post is very wonderful . Thanks once again for the push!
karen millen dress karen millen dresses
Left by karen millen dresses on Apr 20, 2011 9:11 PM

Your comment:
 (will show your gravatar)


Copyright © Eron Wright | Powered by: GeeksWithBlogs.net