由于 String implements IEnumerable<char>
,我期待在 Intellisense 中看到 Enumerable 扩展方法,例如,在输入句点时
String s = "asdf";
s.
我期待看到.Select<char>(...)
,.ToList<char>()
等。然后我惊讶地发现扩展方法实际上确实适用于字符串类,它们只是没有出现在 Intellisense 中。有人知道为什么是这样吗?这可能与这个问题有关。
由于 String implements IEnumerable<char>
,我期待在 Intellisense 中看到 Enumerable 扩展方法,例如,在输入句点时
String s = "asdf";
s.
我期待看到.Select<char>(...)
,.ToList<char>()
等。然后我惊讶地发现扩展方法实际上确实适用于字符串类,它们只是没有出现在 Intellisense 中。有人知道为什么是这样吗?这可能与这个问题有关。
这是通过明确的设计。问题是,虽然 String 最明确地实现了IEnumerable<T>
,但大多数人并没有想到它,或者更重要的是,以这种方式使用它。
String 有相当少的方法。最初我们没有过滤掉 String 的扩展方法,结果是很多负面反馈。通过正确的导入,有时方法的数量几乎增加了两倍。由于显示了所有扩展方法,人们经常无法在噪音中看到他们正在寻找的 String 方法。
String 是一个......简单的类型,最好以这种方式查看它:)
仍然完全可以在字符串上调用扩展方法。它可能不会出现在智能感知中。
编辑: String 实际上有很多方法。但是因为它们中的许多都是重载,所以它们在智能感知中崩溃了。
For info, this has changed in VS2010 (in beta 2, at least). It looks like this filtering has been removed (presumably it caused too much confusion), and the methods are now visible, along with the extension-method glyph.
它应该。
For example you can write it public static string myExtensionMethod(this String yuppi){
}
那么它应该在那里。