Yippie-Ki-Yay
Yippie-Ki-Yay

Reputation: 22804

C# empty property

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

Answers (4)

Richie Cotton
Richie Cotton

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

BrandonZeider
BrandonZeider

Reputation: 8152

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

virtual bool IsEmpty() 
{

}

Upvotes: 0

Jamiec
Jamiec

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

Sergey K
Sergey K

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

Related Questions