Skip to content

Fix IList.Insert example to handle index == Count case#12285

Open
Copilot wants to merge 2 commits intomainfrom
copilot/fix-ilist-insert-end-behavior
Open

Fix IList.Insert example to handle index == Count case#12285
Copilot wants to merge 2 commits intomainfrom
copilot/fix-ilist-insert-end-behavior

Conversation

Copy link
Contributor

Copilot AI commented Feb 6, 2026

The IList.Insert example implementation doesn't handle the documented behavior when index == Count. Per the IList documentation: "If index equals the number of items in the IList, then item is appended to the end."

Changes

  • C# Insert method: Changed condition from index < Count to index <= Count
  • VB.NET Insert method: Changed condition from index < Count to index <= Count

Example

var list = new SimpleList();
list.Add("one");
list.Add("two");

// Before: Insert(2, "three") does nothing (index == Count was rejected)
// After: Insert(2, "three") appends to end as documented
list.Insert(list.Count, "three");
Original prompt

This section details on the original issue you should resolve

<issue_title>IList example does not meet end-of-list Insert behavior</issue_title>
<issue_description>### Type of issue

Code doesn't work

Description

The example Insert implementation does nothing when index==Count. Expected behavior is to add to the end of the list, according to the IList documentation, under "Remarks:"

If index equals the number of items in the IList, then item is appended to the end.

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.collections.ilist.insert?view=net-10.0

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Collections/IList.xml

Document Version Independent Id

29468c75-c596-f099-ce36-2fced37f38b0

Platform Id

441396e6-57a0-1e5d-c43d-8fa0c05bcb1a

Article author

@dotnet-bot</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix IList example to meet end-of-list Insert behavior Fix IList.Insert example to handle index == Count case Feb 6, 2026
Copilot AI requested a review from gewarren February 6, 2026 20:23
@gewarren gewarren marked this pull request as ready for review February 6, 2026 22:53
@gewarren gewarren requested a review from a team as a code owner February 6, 2026 22:53
Copilot AI review requested due to automatic review settings February 6, 2026 22:53
@gewarren gewarren enabled auto-merge (squash) February 6, 2026 22:53
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the IList.Insert example implementations so index == Count is treated as an append, matching the documented IList.Insert behavior.

Changes:

  • Update C# SimpleList.Insert to accept index <= Count.
  • Update VB.NET SimpleList.Insert to accept index <= Count.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
snippets/csharp/System.Collections/IList/Overview/Program.cs Allows Insert(Count, value) to append in the C# snippet.
snippets/visualbasic/System.Collections/IList/Overview/Program.vb Allows Insert(Count, value) to append in the VB snippet (but see noted bug).
Comments suppressed due to low confidence (1)

snippets/visualbasic/System.Collections/IList/Overview/Program.vb:97

  • Insert can throw IndexOutOfRangeException when inserting at index 0 (including into an empty list). The shift loop runs down to index, so when i == 0 it evaluates _contents(i - 1).

Adjust the shift loop so it only copies elements when i > index (or stop at index + 1) so i - 1 never becomes -1, and end-of-list inserts still work.

        If _count + 1 <= _contents.Length AndAlso index <= Count AndAlso index >= 0 Then
            _count += 1

            For i As Integer = Count - 1 To index Step -1
                _contents(i) = _contents(i - 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IList example does not meet end-of-list Insert behavior

2 participants