为什么会出现不带参数没有IsNullOrEmpty重载方法? [英] Why is there no IsNullOrEmpty overload method without parameters?
问题描述
在试图找出一个字符串为null或空,我平时都串了。这就是为什么我本来期望效用函数,如String.IsNullOrEmpty()不带参数的工作:
When trying to figure out if a string is null or empty, I usually have the string already. That's why I would have expected a utility function such as String.IsNullOrEmpty() to work without parameters:
String myString;
bool test=myString.IsNullOrEmpty();
不过,这是不行的,因为IsNullOrEmpty需要一个字符串参数。相反,我必须写:
However, this does not work, because IsNullOrEmpty expects a String parameter. Instead, I have to write:
String myString;
bool test=String.IsNullOrEmpty(myString);
为什么会这样?这似乎不必要的沉闷。当然,我可以很容易地为这个自己的扩展方法,但它似乎是一个非常明显的遗漏,所以我想知道是否有任何好的原因。我不能相信,这个函数的参数的重载刚刚被微软遗忘。
Why is this so? It seems unnecessarily clunky. Of course I can easily write own extension method for this, but it seems like a very obvious omission, so I am wondering if there is any good reason for this. I can't believe that the parameterless overload of this function has just been forgotten by Microsoft.
推荐答案
这个方法已经存在了很长的扩展方法添加到C#之前,而之前扩展方法,没有办法确定一个实例方法/属性,如 xyz.IsNullOrEmpty()
,你仍然可以调用,如果 XYZ
是无效
。
This method has been around long before extension methods were added to C#, and before extension methods, there was no way to define an instance method/property such as xyz.IsNullOrEmpty()
that you could still call if xyz
was null
.
这篇关于为什么会出现不带参数没有IsNullOrEmpty重载方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!