简体   繁体   English

为什么myString.IsNullOrEmpty()没有内置到.Net中?

[英]Why is myString.IsNullOrEmpty() not built into .Net?

A bit of an academic question, but I'm trying to understand the framework design on a deeper level. 这是一个学术问题,但我试图在更深层次上理解框架设计。

So we have String.IsNullOrEmpty(MyString) 所以我们有String.IsNullOrEmpty(MyString)

and we could write an extension method to enable myString.IsNullOrEmpty() , though that's arguably not the greatest idea. 我们可以编写一个扩展方法来启用myString.IsNullOrEmpty() ,尽管这可能不是最好的主意。 See: Is extending String class with IsNullOrEmpty confusing? 请参阅: 使用IsNullOrEmpty扩展String类是否令人困惑? .

So my question is, why doesn't MS write this functionality as part of the .Net framework? 所以我的问题是,为什么MS不将此功能写为.Net框架的一部分? Is there some performance consideration? 有一些性能考虑因素吗? And more generally, why would any method or property viewed as valuable enough to be built as accessible via the String object not be available as a member of any object of the string type? 更一般地说,为什么任何被视为有价值的方法或属性都可以通过String对象构建为不可用作为字符串类型的任何对象的成员?

The static method String.IsNullOrEmpty was introduced in the .NET Framework version 2.0. 静态方法String.IsNullOrEmpty是在.NET Framework 2.0版中引入的。 Extension methods were introduced in the .NET Framework version 3.5 together with LINQ. 与.NETQ一起在.NET Framework 3.5版中引入了扩展方法。 Therefore Microsoft did not have this option when introducing IsNullOrEmpty . 因此,在引入IsNullOrEmpty时,Microsoft没有此选项。

Of course, IsNullOrEmpty cannot be an instance method of String , since you cannot invoke a method on a reference which is null . 当然, IsNullOrEmpty不能是String的实例方法,因为您无法在null的引用上调用方法。 However, you can invoke an extension method on such a reference, since the extension method syntax is just syntactic sugar for a static method invocation. 但是,您可以在此类引用上调用扩展方法,因为扩展方法语法只是静态方法调用的语法糖。


Let's assume that IsNullOrEmpty was an extension method. 我们假设IsNullOrEmpty是一个扩展方法。 Then you could call it like this: 然后你可以这样称呼它:

string s = null;
bool result = s.IsNullOrEmpty();

In a comment, someone pretends that this call would throw a NullReferenceException . 在评论中,有人假装此调用会抛出NullReferenceException The extension method would be declared like this: 扩展方法将声明如下:

public static class StringExtensions
{
    public static bool IsNullOrEmpty(this string s)
    {
        return s == null || s.Length == 0;
    }
}

... and be used like this ... ......并且像这样使用......

 string s = null;
 bool result = s.IsNullOrEmpty();

... which is just syntactic sugar for ... ......这只是...的语法糖

 string s = null;
 bool result = StringExtensions.IsNullOrEmpty(s);

... and thus, would not throw an exception. ......因此,不会抛出异常。 Whether it is a good idea or not to do so is another question (see answer provided by usr below ). 这样做是否是一个好主意是另一个问题(见下面的usr提供的答案 )。

It is generally considered bad-practice to have an extension method not fail when invoked on a null reference. null引用上调用时,扩展方法通常被认为是不好的做法。 This is because just from reading the code you cannot tell that an extension method is being called. 这是因为仅仅通过阅读代码就无法判断是否正在调用扩展方法。 Your intuition would be to see the call ((string)null).IsNullOrEmpty() fail. 你的直觉是看到调用((string)null).IsNullOrEmpty()失败。

Obviously, this kind of method would not be possible as an instance method. 显然,这种方法不可能作为实例方法。 So we are violating intuition here. 所以我们在这里违反了直觉。

That said, I have defined exactly this extension in all important projects of mine and it is outrageously useful in a lot of cases. 也就是说,我已经在我的所有重要项目中确切地定义了这个扩展,并且在很多情况下它非常有用。 I am willing to accept this small level of impurity and unintuitiveness. 我愿意接受这种微小的杂质和不直观。

The framework authors obviously disagreed. 框架作者显然不同意。 I also think that this method should not go into the .NET Framework because it is kind of "advanced" and impedes learnability. 我也认为这种方法不应该进入.NET Framework,因为它有点“高级”并且阻碍了可学习性。 A beginner might ask "Huh? Sometimes I can invoke a method on a null reference safely, sometimes I can't? How to tell when?". 一个初学者可能会问“嗯?有时候我可以安全地调用null引用上的方法,有时候我不能?如何判断何时?”。

Because if you could use IsNullOrEmpty() on a null string, then you probably might want to rename the method to IsEmpty() . 因为如果您可以在空字符串上使用IsNullOrEmpty() ,那么您可能希望将该方法重命名为IsEmpty()

Jokes aside, this is how the method is implemented: 除了笑话,这是方法的实现方式:

public static bool IsNullOrEmpty(string value)
{
  if (value != null)
    return value.Length == 0;
  else
    return true;
}

It's clear that the condition would always be true in case of string instances. 很明显,在字符串实例的情况下,条件总是如此。

Also, one little detail. 还有,一个小细节。 Take, for instance, string.Concat which is a static method. string.Concat例子, string.Concat是一个静态方法。 It could've been reasonable, for example, wonder why there's no relative instance method; 例如,它可能是合理的,想知道为什么没有相对实例方法; by looking at its implementation, I believe that they wanted to make those methods as fail-proof as possible. 通过查看其实现,我相信他们希望尽可能地使这些方法成为失败证明。 When passing the arguments to the method, in case of null references, they are replaced with empty strings instead of having exceptions thrown. 将参数传递给方法时,如果是空引用,则将它们替换为空字符串,而不是抛出异常。 This can be useful when you don't know in advance if your string(s) will actually contain a value or be null, and I guess that the framework's developers decided that it'd be better to contribute to code readability by treating null strings as empty ones and saving the end-user an additional check. 当你事先不知道你的字符串是否实际包含一个值或者为null时,这可能很有用,我猜这个框架的开发人员认为通过处理空字符串来提高代码可读性会更好作为空的并保存最终用户额外的检查。 Surely, if string.Concat was an instance method (or at least had an alternative), the user could still pass null arguments, but the instance which is being operated on will necessarily be not null. 当然,如果string.Concat是一个实例方法(或者至少有一个替代方法),用户仍然可以传递空参数,但正在操作的实例必然不是null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 String.IsNullOrEmpty(myString)vs myString!= null - String.IsNullOrEmpty(myString) Vs myString != null string.IsNullOrEmpty(myString)或string.IsNullOrWhiteSpace(myString)是否违反了SRP规则? - string.IsNullOrEmpty(myString) or string.IsNullOrWhiteSpace(myString) is not violating SRP Rule? string.IsNullOrEmpty(myString.Trim()) 与 string.IsNullOrWhiteSpace(myString) - string.IsNullOrEmpty(myString.Trim()) vs string.IsNullOrWhiteSpace(myString) 为什么要创建string.IsNullOrEmpty()? - why string.IsNullOrEmpty() is created? 为什么String.IsNullOrEmpty(str)而不是str.IsNullOrEmpty()? - Why String.IsNullOrEmpty(str) and not str.IsNullOrEmpty()? 为什么没有没有参数的IsNullOrEmpty重载方法? - Why is there no IsNullOrEmpty overload method without parameters? .NET 3.0或更高版本中是否修复了IsNullOrEmpty错误? - Is the IsNullOrEmpty bug fixed in .NET 3.0 or later? 为什么没有 Guid.IsNullOrEmpty() 方法 - Why isn't there a Guid.IsNullOrEmpty() method 为什么在Visual Studio 2015中string.IsNullOrEmpty优于String.IsNullOrEmpty? - Why in Visual Studio 2015 string.IsNullOrEmpty is better than String.IsNullOrEmpty? 为什么String.IsNullOrEmpty比String.Length快? - Why is String.IsNullOrEmpty faster than String.Length?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM