Skip to content

How to Merge and Concatenate Arrays in C#: fix two merge methods, net10.0 - #2277

Merged
vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/72907-csharp-merge-arrays
Oct 11, 2026
Merged

vladimir-pecanac-main merged 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/72907-csharp-merge-arrays

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates the sample for the article "How to Merge and Concatenate Arrays in C#" (https://code-maze.com/csharp-merge-arrays/). Only collections-arrays/HowToMergeArrays changes.

  • Both projects move from net6.0 to net10.0.
  • Packages, re-queried on NuGet on 2026-10-10 (latest stable): BenchmarkDotNet 0.15.8, Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.1, coverlet.collector 10.1.0. The folder keeps xunit.
  • MergeUsingBlockCopy passed element counts to Buffer.BlockCopy, whose offsets and count are in bytes, so it copied a quarter of each array and left the rest zero (the .NET 10 SDK flagged both calls with CA2018). The offsets and counts are now multiplied by sizeof(int).
  • MergeUsingArrayCopyWithResize resized Array.Empty<int>() instead of the first array, so the first array's values never reached the result. It now resizes a reference to firstArray and copies the second array after it; firstArray itself is left unchanged.
  • The tests asserted only NotEmpty and Length, and both broken methods passed them. Every test now compares the merged contents with an expected array built without any method under test. On the old method bodies the new tests fail exactly twice (the BlockCopy and Resize tests); on the fixed bodies all 11 pass.
  • The benchmark source arrays were 1,000 copies of 1 and 1,000 copies of 2, so Union returned a two-element array. They are now Enumerable.Range(0, n) and Enumerable.Range(n / 2, n), so Union returns 1.5n values and measures comparable work.
  • _arraySize goes back from 50000 to 1000, which is the value the article prints before it asks the reader to change it to 50,000.
  • New benchmarks MergeUsingSpanCopyTo and MergeUsingCollectionExpression (C# 12 spread), each with a test.
  • The SelectMany lambda parameter animal is renamed to array.

Build: 0 warnings, 0 errors. Tests: 11 of 11 passing. dotnet list package --vulnerable --include-transitive: no vulnerable packages. SDK 10.0.302, runtime 10.0.10.

…retarget to net10.0

MergeUsingBlockCopy passed element counts where Buffer.BlockCopy expects bytes, so it copied a
quarter of each array (CA2018). MergeUsingArrayCopyWithResize resized an empty array instead of
the first one, so the first array never reached the result. Both now merge correctly.

Every test now checks the merged contents, not only the length; the old tests passed on both
broken methods. The source arrays overlap by half so Union merges comparable work, and
_arraySize is back to 1000 to match the article. Adds MergeUsingSpanCopyTo and
MergeUsingCollectionExpression, renames the SelectMany lambda parameter, and moves both
projects to net10.0 with current packages.
@vladimir-pecanac-main
vladimir-pecanac-main merged commit 2c7d08a into CodeMazeBlog:main Oct 11, 2026
4 checks passed
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.

1 participant