开发者

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?


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.


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


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

virtual bool IsEmpty() 
{

}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜