为什么C#.NET SortedList <T1,T2>实际上没有ElementAt?

Way*_*ner 4 .net c# sortedlist

3.5 Collections.Generic.SortedList的.NET文档

在文档中,它明确指出"ElementAt"是SortedList成员的扩展方法.嗯,我有一个,因此宣布:

private SortedList<int, ChainLink> linksByLevel = new SortedList<int, ChainLink>();
Run Code Online (Sandbox Code Playgroud)

我试着得到最后一个元素:

ChainLink lastLink = linksByLevel.ElementAt(linksByLevel.Count - 1);
Run Code Online (Sandbox Code Playgroud)

编译器会抛出大量有用的消息:

错误1'System.Collections.Generic.SortedList'不包含'ElementAt'的定义,并且没有System.Collections.Generic.SortedList<int,ChainLink>'可以找到接受类型'的第一个参数的扩展方法'ElementAt' (您是否缺少using指令或程序集引用?)

我得到由微软的文档,我的编译器在缺乏连贯性非常沮丧,很想咆哮有关的API之间的矛盾SortedListSortedList<T1, T2>,但我怀疑这会增加多少价值,我的问题.只相信我,这很令人沮丧: -

Dan*_*ker 14

尝试将导入添加到代码文件的顶部:

using System.Linq;
Run Code Online (Sandbox Code Playgroud)

在实际使用扩展方法之前,您需要指定您正在使用包含扩展方法的命名空间.所以,正如错误解释的那样,你错过了一个指导using.

如果您确实知道该方法是一种扩展方法,但您不知道它所在的命名空间,那么您可能需要在线搜索才能找到它.默认情况下,Visual Studio 2012无法为您解决此问题.您正在讨论的扩展方法位于System.Core.dll Enumerable.ElementAtSystem.Linq命名空间中.

在Visual Studio中,当您创建新的类文件时,您将using System.Linq自动插入顶部.该命名空间包含用于处理各种集合(包括列表,字典和数组)的所有LINQ扩展方法.