Repository navigation
How to Merge and Concatenate Arrays in C#: fix two merge methods, net10.0 - #2277
Merged
vladimir-pecanac-main merged 2 commits intoOct 11, 2026
Conversation
…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.
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.
Updates the sample for the article "How to Merge and Concatenate Arrays in C#" (https://code-maze.com/csharp-merge-arrays/). Only
collections-arrays/HowToMergeArrayschanges.MergeUsingBlockCopypassed element counts toBuffer.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 bysizeof(int).MergeUsingArrayCopyWithResizeresizedArray.Empty<int>()instead of the first array, so the first array's values never reached the result. It now resizes a reference tofirstArrayand copies the second array after it;firstArrayitself is left unchanged.NotEmptyandLength, 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.Unionreturned a two-element array. They are nowEnumerable.Range(0, n)andEnumerable.Range(n / 2, n), soUnionreturns 1.5n values and measures comparable work._arraySizegoes back from 50000 to 1000, which is the value the article prints before it asks the reader to change it to 50,000.MergeUsingSpanCopyToandMergeUsingCollectionExpression(C# 12 spread), each with a test.SelectManylambda parameteranimalis renamed toarray.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.