4

.NET Documentation for 3.5 Collections.Generic.SortedList

In the documentation, it plainly states that "ElementAt" is an extension method on SortedList members. Well, I've got one, declared thusly:

private SortedList<int, ChainLink> linksByLevel = new SortedList<int, ChainLink>();

I try to get the last element:

ChainLink lastLink = linksByLevel.ElementAt(linksByLevel.Count - 1);

The compiler throws the massively helpful message:

Error 1 'System.Collections.Generic.SortedList' does not contain a definition for 'ElementAt' and no extension method 'ElementAt' accepting a first argument of type 'System.Collections.Generic.SortedList<int,ChainLink>' could be found (are you missing a using directive or an assembly reference?)

I'm getting pretty frustrated by the lack of coherence in Microsoft's documentation and my compiler and would love to rant about the inconsistencies between the APIs for SortedList and SortedList<T1, T2>, but I doubt that would add much value to my question. Just trust me, it's frustrating :-\

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290

3 Answers3

14

Try adding an import to the top of your code file:

using System.Linq;

You need to specify that you're using the namespace containing the extension method before you can actually use the extension method. So, as the error explained, you were missing a using-directive.

If you do know that the method is an extension method, but you don't know in which namespace it lives, then you'll probably need to search online to find it. Visual Studio 2012 by default cannot resolve it for you. The extension method you are talking about is Enumerable.ElementAt in the System.Linq namespace of System.Core.dll.

In Visual Studio, when you create a new class file, you'll have using System.Linq inserted at the top automatically. That namespace contains all the LINQ extension methods for working with all kinds of collections (including lists, dictionaries and arrays).

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
  • +1 good answer. But I have to nitpick. The using directive is not an "import" and no "importing" happens. The using directive is an alias'ing mechanism. http://tergiver.wordpress.com/2011/03/11/understanding-the-using-directive/ – Tergiver Aug 02 '12 at 21:22
  • 1
    Granted, extension methods are a little weird because they simply don't exist if their containing namespace is not aliased. – Tergiver Aug 02 '12 at 21:27
  • Well, I agree with you so I adjusted it. – Daniel A.A. Pelsmaeker Aug 02 '12 at 21:32
  • @Tergiver, and the worst part is that in the documentation where the extension method was referenced (see my link), there was *no mention of System.Linq*. When I read a document titled "SortedList Members", I expect them to refer to things that are available everywhere I can create a new SortedList, or use said SortedList. – Wayne Werner Aug 03 '12 at 18:52
4

Did you add

using System.Linq;

at the top of your file?

W.R.T. documentation inconsistency, if you look at the SortedList docs, and click through to ElementAt, you'll clearly see at the top of the page that you need to include System.Linq.

spender
  • 117,338
  • 33
  • 229
  • 351
  • There's no need to [RTFM](http://en.wikipedia.org/wiki/RTFM). Apparently the poster is new with extension methods and did not understand that even if he read it. RTFM is just not helpful. – Daniel A.A. Pelsmaeker Aug 02 '12 at 19:15
  • Yes, you're right. Not meant vindictively. Corrected. I felt the slightly ranty nature of the original post was misplaced though. – spender Aug 02 '12 at 19:17
  • The ranty nature was actually due to the fact that I *was* RTFM, and the *only* place that namespace was mentioned at all was a teensy link at the top (up where no essential information is, right?) of the documentation for the Extension method. When I clicked on the link titled "SortedList Members" I expect those members to be in the same namespace. Nowhere in the SortedList docs could I find a reference to System.Linq. But now I know better - Microsoft doesn't say "This stuff comes from here, and click to find out how to use it." Instead... – Wayne Werner Aug 03 '12 at 18:48
  • "Here is something," leaving it up to me to find out where to get it. (Also I was already frustrated by the apparent inconsistencies between two identically named classes. I kind of hate that.) – Wayne Werner Aug 03 '12 at 18:50
2

SortedList and SortedList<K,V> are very different beasts.

The older SortedList uses an array under the covers and so it can provide an efficient indexer.

Beware that when you use the extension method ElementAt() it will probably do a 'tablescan', don't use it for something time-critical.

spender
  • 117,338
  • 33
  • 229
  • 351
H H
  • 263,252
  • 30
  • 330
  • 514