"'System.ArgumentOutOfRangeException" on inserting of element to reserved position of List

Viewed 61

I have the List of Count equals 2 but currently both positions are empty:

System.Diagnostics.Debug.WriteLine(paginationCollection.PagesContent.Count); // 0
System.Diagnostics.Debug.WriteLine(paginationCollection.PagesContent.Capacity); // 2

Here is, the paginationCollection is the instance of

public class PaginationCollection<Item>
{

  public readonly uint PagesCount;

  public List<List<Item>> PagesContent;

  public PaginationCollection(
    uint pagesCount,
    byte numerationFrom
  )
  {
    
    PagesCount = pagesCount;
    PagesContent = new List<List<Item>>(numerationFrom == 0 ? (int)pagesCount : (int)pagesCount + 1);
  }
}

When I try to add some items to position number 1, I get theSystem.ArgumentOutOfRangeException:

for (int pageNumber = pagesNumerationFrom; pageNumber < lastPageNumber; pageNumber++)
{

  paginationCollection.PagesContent.Insert(
    pageNumber, // 1 for current data
    // ↓ Confirmed that GetRange operation does not cause the Exception
    flatCollection.GetRange((int)elementStartingPositionForCurrentPage, (int)elementEndingPositionForCurrentPage) 
  );

   // ...
}

From the viewpoint of logic, the position number 1 is currently empty, but the inserting operation number be available (AFAIK in C# case, the Capacity must be greater or equal to target position).

0 Answers
Related