Skip to content

Fix Collection builder example to be well-behaved with proper buffer allocation #47994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 18, 2025

The LineBuffer class in the Collection builder example was not well-behaved according to the documentation's own definition. It had two main issues:

  1. Enumeration mismatch: When creating a collection with a few elements, it would enumerate all 80 elements from its internal buffer, violating the principle that "The value of Count or Length on a countable collection produces the same value as the number of elements when enumerated."

  2. Memory waste: It always allocated a fixed 80-character buffer regardless of actual usage, wasting memory for smaller collections.

Before this fix:

LineBuffer line = ['H', 'e', 'l', 'l', 'o'];  // 5 elements
// - Allocates 80 characters (wastes 75)
// - foreach would enumerate 80 elements instead of 5
// - No Count property available

After this fix:

LineBuffer line = ['H', 'e', 'l', 'l', 'o'];  // 5 elements
Console.WriteLine(line.Count);                 // 5
// - Allocates exactly 5 characters
// - foreach enumerates exactly 5 elements
// - Proper indexer access with bounds checking

Changes made:

  • Added _count field to track the actual number of elements set in the constructor
  • Changed buffer allocation from fixed new char[80] to dynamic new char[buffer.Length]
  • Added Count property to make the collection countable
  • Modified GetEnumerator() to only yield actual elements using yield return
  • Added indexer this[int index] with proper bounds checking for direct element access
  • Fixed IEnumerable.GetEnumerator() to delegate to the generic version

The collection now properly demonstrates the "well-behaved collection" concept with efficient memory usage and correct enumeration behavior.

Fixes #41756.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@dotnet-policy-service dotnet-policy-service bot added the okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. label Aug 18, 2025
@Copilot Copilot AI changed the title [WIP] Problem with the Collection builder example Fix Collection builder example to be well-behaved Aug 18, 2025
@Copilot Copilot AI requested a review from BillWagner August 18, 2025 14:51
Copilot finished work on behalf of BillWagner August 18, 2025 14:51
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

Good start, but the code needs more updates.

@BillWagner
Copy link
Member

@copilot Can you address the remaining comment?

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Aug 19, 2025

@copilot Can you address the remaining comment?

Fixed the buffer allocation issue. The LineBuffer now allocates exactly the buffer size needed instead of a fixed 80-character array. Changes made in commit 425a8b2:

  • Removed the fixed allocation = new char[80]
  • Updated constructor to allocate _buffer = new char[buffer.Length]
  • Simplified the logic to copy all elements since buffer sizes now match

The collection now properly allocates only the memory it needs while maintaining the well-behaved collection behavior.

@Copilot Copilot AI changed the title Fix Collection builder example to be well-behaved Fix Collection builder example to be well-behaved with proper buffer allocation Aug 19, 2025
@Copilot Copilot AI requested a review from BillWagner August 19, 2025 14:08
Copilot finished work on behalf of BillWagner August 19, 2025 14:08
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

This is now ready for final review

@BillWagner BillWagner marked this pull request as ready for review August 19, 2025 14:24
@BillWagner BillWagner requested a review from a team as a code owner August 19, 2025 14:24
@BillWagner BillWagner requested a review from adegeo August 19, 2025 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-csharp/svc lang-reference/subsvc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problem with the Collection builder example
2 participants