> In this particular example I would chose a function over a property.
The reason is because a lot (most?) people assume that properties are
constant time operations.
I don't know about "constant time operations," but it's true that
properties shouldn't take much time to execute. That said, I have many,
many properties in the framework I built at my company that could, in
theory, go back to the database for information, so they're waaaay over
"constant time."
You see, it's not always so cut-and-dried.
On the one hand, if you make something a property, you are implying
that getting the information will be quick. So, if getting the
information is slow, that is a hint that it shouldn't be a property.
On the other hand, you don't want to determine what is a property or
not based upon implementation details. By doing so you're letting the
internal guts of how your class works leak out into its interface with
the outside world. That's bad style. So, something should be a property
if it makes sense that it be a property.
Certainly, anything that affects the state of the object in a manner
more sophisticated than just "set this to that" should be a method.
That's painfully obvious.
Similarly, an aspect of your class that can be read and set, even if
setting it does more than just set a variable, should probably be a
property. Reasonably straightfoward.
However, sometimes the two design goals above-properties that return
results quickly and not letting implementation determine
interface-are at odds with each other, and you have to make a
judgement call.
In the case of my system, I had things that logically should be
properties (from an interface point of view) but the object held only
keys, not references to the objects themselves, so the "get" on the
property has to fetch the object in question from the database. It was
an open question whether to code:
Supplier theSupplier = stockItem.Suppl ier;
or
Supplier theSupplier = stockItem.GetSu pplier();
I chose the former, preferring to hide the implementation (holding
supplier UID, not supplier itself) rather than let it leak out that
getting the supplier was actually a lot of work. In our case it's no
problem, because we're a very small shop. In a different situation I
might have gone for door #2.
In your case, I would definitely make it a property. The fact that you
have to loop through some stuff is an implementation detail and, as
Lucian pointed out, you could always cache the result to avoid
recalculating it the second and subsequent times. You might even be
able to keep the "answer" up to date internally as your object is
modified.
That's why the design guideline about not choosing property / method
based on implementation is there: because next month you may think of a
different implementation that is much faster, but you've already made
it a method when it would have made more sense as a property.