Fix IList.Insert example to handle index == Count case#12285
Open
Fix IList.Insert example to handle index == Count case#12285
Conversation
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
Contributor
There was a problem hiding this comment.
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.Insertto acceptindex <= Count. - Update VB.NET
SimpleList.Insertto acceptindex <= 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
Insertcan throwIndexOutOfRangeExceptionwhen inserting at index 0 (including into an empty list). The shift loop runs down toindex, so wheni == 0it 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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
index < Counttoindex <= Countindex < Counttoindex <= CountExample
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.