Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 13, 2025

📋 Issue Reference

Fixes #102

🔍 Analysis Summary

This PR provides a comprehensive comparison between Platform.Collections' ArrayString<T> and .NET's ArraySegment<T>. Both types serve similar purposes but have important architectural and behavioral differences.

🏗️ Key Architectural Differences

Type Design

  • ArrayString: Reference type (class) inheriting from Segment<T>
  • ArraySegment: Value type (struct) from .NET BCL

Constructors

  • ArrayString:
    • new ArrayString<T>(length) - Creates new array
    • new ArrayString<T>(array) - Wraps entire array from index 0
    • new ArrayString<T>(array, length) - Wraps first N elements from index 0
  • ArraySegment:
    • new ArraySegment<T>(array) - Wraps entire array
    • new ArraySegment<T>(array, offset, count) - Wraps segment at any offset

Segmentation Capabilities

  • ArrayString: Always starts from index 0, can only specify length
  • ArraySegment: Can start from any offset with any count

🔄 Behavioral Similarities

Interface Implementation

Both implement IList<T>, ICollection<T>, IEnumerable<T> with read-only behavior:

  • Indexer access (get/set) ✅
  • Enumeration support ✅
  • Modification operations throw NotSupportedException

Memory Efficiency

Both provide zero-copy views over existing arrays without data duplication.

⚖️ Key Differences

Equality & Hashing

  • ArrayString: Content-based equality using EqualTo() extension method
  • ArraySegment: Reference-based equality (same array + same offset/count)

Memory Allocation

  • ArrayString: Can create new arrays or wrap existing ones
  • ArraySegment: Only wraps existing arrays (no allocation)

Performance Characteristics

  • ArrayString: Heap allocation overhead, GC pressure
  • ArraySegment: Stack allocation, value type copying overhead when passed by value

Default Values

  • ArrayString: default is null
  • ArraySegment: default is empty segment with Array = null

🧪 Implementation Details

The comparison is implemented as a comprehensive test suite in ArrayStringVsArraySegmentComparisonTests.cs covering:

  • Constructor variations and limitations
  • Type system differences (class vs struct)
  • Interface implementations and capabilities
  • Read-only behavior enforcement
  • Indexer and enumeration functionality
  • Segmentation patterns and constraints
  • Equality and hash code generation
  • Memory allocation patterns
  • Default value handling
  • Performance characteristics documentation

📊 Usage Recommendations

Choose ArrayString when:

  • You need content-based equality semantics
  • You want to create new arrays with specific lengths
  • You always work with segments from index 0
  • You prefer reference type semantics

Choose ArraySegment when:

  • You need arbitrary offset-based segments
  • You want minimal allocation overhead
  • You prefer value type semantics
  • You need .NET BCL consistency

✅ Testing

All comparison tests pass, demonstrating the documented behavior differences and similarities between the two types.


🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #102
@konard konard self-assigned this Sep 13, 2025
…ment<T>

- Created detailed test suite documenting similarities and differences
- Covers constructors, type characteristics, interfaces, behavior patterns
- Documents equality, hashing, memory allocation, and performance traits
- Provides practical examples for understanding usage patterns
- Addresses issue #102 requirement to compare these two collection types

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@konard konard changed the title [WIP] Compare ArrayString and ArraySegment from .NET 5 Compare ArrayString<T> and ArraySegment<T> from .NET Sep 13, 2025
@konard konard marked this pull request as ready for review September 13, 2025 17:32
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.

Compare ArrayString and ArraySegment from .NET 5

2 participants