本文介绍了在C#中列出GetRange错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理列表,我的列表有14条记录.

i am working with list and my list has 14 record.

 List<Product> oProduct = new List<Product> 
        {
           new Product("../images/1.jpg", "Sample Data.1"),
           new Product("../images/2.jpg", "Sample Data.2"),
           new Product("../images/3.jpg", "Sample Data.3"),
           new Product("../images/4.jpg", "Sample Data.4"),
           new Product("../images/5.jpg", "Sample Data.5"),
           new Product("../images/6.jpg", "Sample Data.6"),
           new Product("../images/7.jpg", "Sample Data.7"),
           new Product("../images/8.jpg", "Sample Data.8"),
           new Product("../images/9.jpg", "Sample Data.9"),
           new Product("../images/10.jpg", "Sample Data.10"),
           new Product("../images/11.jpg", "Sample Data.11"),
           new Product("../images/12.jpg", "Sample Data.12"),
           new Product("../images/13.jpg", "Sample Data.13"),
           new Product("../images/14.jpg", "Sample Data.14"),
        };

当我将以下行用于getrange时,我的索引超出了范围错误.

when i use the below line for getrange then i am getting index out of bound error.

List<Product> xProduct = oProduct.GetRange(10, 13);

但是我的列表中有14个元素,那么为什么我不能从第10位到第14位提取数据....请指导谢谢.

but my list has 14 element then why i can not extract data from 10th position to 14th position....please guide thanks.

推荐答案

GetRange 必须是要获取的元素的 count ,因此请将其更改为 4 (我想这就是您想要的).

The second parameter to GetRange needs to be the count of elements to get, so change it to 4 (I think that's what you want).

此外,第一个参数是基于的索引,因此您希望 GetRange(9,4)获取10到13的图像.

Also, the first parameter is the zero-based index, so you want GetRange(9, 4) to get images 10 through 13.

这篇关于在C#中列出GetRange错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 13:03