views:

768

answers:

3

Since String implements IEnumerable<char>, I was expecting to see the Enumerable extension methods in Intellisense, for example, when typing the period in

String s = "asdf";
s.

I was expecting to see .Select<char>(...), .ToList<char>(), etc. I was then suprised to see that the extension methods do in fact work on the string class, they just don't show up in Intellisense. Does anyone know why this is? This may be related to this question.

+13  A: 

It's by explicit design. The problem is that while String most definitely implements IEnumerable<T>, most people don't think of it, or more importantly use it, in that way.

String has a fairly small number of methods. Initially we did not filter extension methods off of String and the result was a lot of negative feedback. It almost tripled the number of methods at times with the right imports. With all of the extension methods displayed, people often couldn't see the String method they were looking for in the noise.

String is a ... simple type and it's better to view it that way :)

It's still completely possible to call an extension method on string. It's just likely not going to show up in intellisense.

EDIT: String actually has quite a few methods. But because many of them are overloads they collapse in intellisense.

JaredPar
Is this hard-coded in VS, or is there some attribute used on the String or Enumerable class that we can use to accomplish the same effect?
foson
I do not believe there is an attribute or config that can be tweaked to stop or get this behavior.
JaredPar
This should really be configurable. If you're going to have extension methods then I think you really just need to live with the nonsense. That just seems like a very non-obvious and unexpected behavior.
BobbyShaftoe
"String has a fairly small number of methods" - really? I count about 42 before extension methods. Yes, extension methods add rather a lot, but 42 methods is still a pretty large number to start with!
Jon Skeet
The same "noise" problem exists for any IEnumerable-implementing class - why only fix it for strings? The autocomplete popup needs buttons down the side of it. Show/hide methods, properties, events, extension methods. Sort options: by member kind, alphabetical. And all would have keyboard shortcuts.
Daniel Earwicker
@Jon, I should have said "fairly small number of methods by name." In intellisense all of the overloads are collapsed to one item until you actually select the item and start typing parameters.
JaredPar
@Earwicker, Goes back to expectation. The general user is quite surprised to find out that string is an IEnumerable<char> and feedback was enough that a design change was made.
JaredPar
Quick note: This post is reflecting my recollection of why this decision was made during the VS2008 vs. my opinion on how it should be. I didn't actually participate in this particular decision process. I do however like it this way but I'm mostly ambivalent on the subject
JaredPar
@Jared: That count of ~42 was *before* taking overloads into account. That was just the number of methods by name! With all the overloads, it has *masses* of methods :)
Jon Skeet
@Jon, My ability to visually count is off. I ran a quick powershell script "([String]::Empty|gm).Count" and it produced a count of 37 (2 are property getters).
JaredPar
A: 

It should.

For example you can write it public static string myExtensionMethod(this String yuppi){
}

Then it is supposed to be there.

Braveyard
what ? it's not working !
Braveyard
+1  A: 

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.

Marc Gravell
A more intelligent Intellisense. Excellent.
foson