Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List GetRange error in C#

Tags:

c#

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"),
        };

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

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

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

like image 858
Thomas Avatar asked Dec 02 '22 01:12

Thomas


2 Answers

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).

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

like image 200
AakashM Avatar answered Dec 04 '22 22:12

AakashM


List<T>.GetRange takes start and count, not start and end. If you want elements 10-13, use GetRange(10, 4).

like image 30
Adam Robinson Avatar answered Dec 04 '22 22:12

Adam Robinson