Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中通过索引获取列表元素#_C#_List - Fatal编程技术网

C# 在C中通过索引获取列表元素#

C# 在C中通过索引获取列表元素#,c#,list,C#,List,我正在尝试访问C#中的列表项。我有一个列表作为list。我必须将列表的前三个元素(整数数组)分配给变量。这就是我所做的 int[] c1 = lst.ElementAt(0); int[] c2 = lst.ElementAt(1); int[] c3 = lst.ElementAt(2); 我还尝试通过lst[0]、lst[1]、lst[2]而不是ElementAt()来访问它。尽管列表的前三个元素彼此不同,但所有变量都取列表第一项的值。我通过调试检查了值。我做错了什么?我脑海中的另一个问题

我正在尝试访问C#中的列表项。我有一个列表作为
list
。我必须将列表的前三个元素(整数数组)分配给变量。这就是我所做的

int[] c1 = lst.ElementAt(0);
int[] c2 = lst.ElementAt(1);
int[] c3 = lst.ElementAt(2);

我还尝试通过
lst[0]、lst[1]、lst[2]
而不是
ElementAt()
来访问它。尽管列表的前三个元素彼此不同,但所有变量都取列表第一项的值。我通过调试检查了值。我做错了什么?我脑海中的另一个问题是,
lst[0]
lst.ElementAt(0)
你做错了什么。因为ElementAt的默认实现如下所示:

 public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, int index) {
        if (source == null) throw Error.ArgumentNull("source");
        IList<TSource> list = source as IList<TSource>;
        if (list != null) return list[index];
        if (index < 0) throw Error.ArgumentOutOfRange("index");
        using (IEnumerator<TSource> e = source.GetEnumerator()) {
            while (true) {
                if (!e.MoveNext()) throw Error.ArgumentOutOfRange("index");
                if (index == 0) return e.Current;
                index--;
            }
        }
    }
publicstatictsourceelementat(此IEnumerable源代码,int索引){
if(source==null)抛出错误.ArgumentNull(“source”);
IList list=源作为IList;
if(list!=null)返回列表[索引];
如果(索引<0)抛出错误。ArgumentOutOfRange(“索引”);
使用(IEnumerator e=source.GetEnumerator()){
while(true){
如果(!e.MoveNext())抛出错误.ArgumentOutOfRange(“索引”);
如果(索引==0)返回当前值;
索引--;
}
}
}

对于实现IList的任何东西,它都将使用与您相同的索引器,因为您做错了什么。因为ElementAt的默认实现如下所示:

 public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, int index) {
        if (source == null) throw Error.ArgumentNull("source");
        IList<TSource> list = source as IList<TSource>;
        if (list != null) return list[index];
        if (index < 0) throw Error.ArgumentOutOfRange("index");
        using (IEnumerator<TSource> e = source.GetEnumerator()) {
            while (true) {
                if (!e.MoveNext()) throw Error.ArgumentOutOfRange("index");
                if (index == 0) return e.Current;
                index--;
            }
        }
    }
publicstatictsourceelementat(此IEnumerable源代码,int索引){
if(source==null)抛出错误.ArgumentNull(“source”);
IList list=源作为IList;
if(list!=null)返回列表[索引];
如果(索引<0)抛出错误。ArgumentOutOfRange(“索引”);
使用(IEnumerator e=source.GetEnumerator()){
while(true){
如果(!e.MoveNext())抛出错误.ArgumentOutOfRange(“索引”);
如果(索引==0)返回当前值;
索引--;
}
}
}

对于实现IList的任何内容,它将使用与您相同的索引器

请发布您的代码如何填充列表。显示您正在用值填充列表的代码-这可能是一个参考问题:-)问题可能是您如何填充列表。我的猜测是,在位置1、2和3有相同的整数数组。关于你的后续问题,请看没有区别
ElementAt()
用于像
IEnumerable
这样的集合,但对于list,您只需使用
[]
<代码>列表实现了
IEnumerable
,因此您可以同时使用这两者。ElementAt在内部使用[],速度较慢。它用于IEnumerable而不是具体类型,如List。请发布您的代码如何填充列表。显示您正在用值填充列表的代码-这可能是一个参考问题:-)问题可能是如何填充列表。我的猜测是,在位置1、2和3有相同的整数数组。关于你的后续问题,请看没有区别
ElementAt()
用于像
IEnumerable
这样的集合,但对于list,您只需使用
[]
<代码>列表实现了
IEnumerable
,因此您可以同时使用这两者。ElementAt在内部使用[],速度较慢。它更多地用于IEnumerable而不是具体类型,如List。这并不能回答问题all@victor这实际上是一个完整而正确的答案。这证明了两种访问方法是相同的,问题无法再现,OP可能在实际问题中遗漏了代码。你可能应该要求OP解释实际的问题结果是一样的,但无论如何它们都不相同。我错过了OP说他的代码有错误的评论,所以这实际上回答了他问题的剩余部分,更正了这根本没有回答问题all@victor这实际上是一个完整而正确的答案。这证明了两种访问方法是相同的,问题无法再现,OP可能在实际问题中遗漏了代码。你可能应该要求OP解释实际的问题结果是一样的,但无论如何它们都不相同。我错过了OP说他的代码有错误的评论,所以这实际上回答了他问题的剩余部分,修正了