3

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?

Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148

4 Answers4

14

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.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
2

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()

Sergey K
  • 4,071
  • 2
  • 23
  • 34
0

You're right, it's definitely personal choice, and mine would be:

virtual bool IsEmpty() 
{

}
BrandonZeider
  • 8,014
  • 2
  • 23
  • 20
0

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.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360