Reputation: 22804
What's the preferred way to implement IsEmpty
statement for your own container-like class?
It could be a simple method bool IsEmpty()
or you could have some gettable property IsEmpty
/ Empty
.
I understand it's probably a matter a personal choice, but would you stick to properties or methods in such cases?
Upvotes: 3
Views: 886
Reputation: 121057
Searching for IsEmpty
in VS 2008's help reveals 40 properties and 11 methods. (Your mileage may vary, depending upon version.) So either is acceptable, but property is more common.
The most important thing is to be consistent throughout your code.
Upvotes: 0
Reputation: 8152
You're right, it's definitely personal choice, and mine would be:
virtual bool IsEmpty()
{
}
Upvotes: 0
Reputation: 136074
The general rule is if it is costly, or has side effects then make it a method. If it just reads a field make it a property.
Upvotes: 14
Reputation: 4114
I will be use readonly property IsEmpty if it is simple accessor to private field if you have some algorithm to determine if something is empty you should use the method IsEmpty()
Upvotes: 2