Skip to content

Array Slicing in C#: retarget SliceArrays to .NET 10 and fix the crash on run - #2261

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/66876-csharp-array-slicing
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/66876-csharp-array-slicing

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample update for the article "Array Slicing in C#" (https://code-maze.com/csharp-array-slicing/). Only collections-arrays/SliceArrays changes.

Changes:

  • Both projects move from net6.0 to net10.0.
  • The program no longer crashes on dotnet run. Main used to call only SliceArrayUsingRangeOperator, whose last line array[3..1] throws ArgumentOutOfRangeException. That line now lives in SliceArrayUsingReversedRange inside try/catch and prints the exception type. Every demonstration method is now called.
  • array[..3] becomes array[..2] to match the article, and the test assertion moves from 3 to 2.
  • Top-level statements replace namespace, class Program and static void Main.
  • New demonstrations and tests: AsSpan(0, 3) and AsSpan(3), Span.Slice(0, 2), data.AsSpan()[^2..], List slicing with list[1..3], GetRange and CollectionsMarshal.AsSpan, and string slicing with text[5..] and text.AsSpan(5). A test pins the reversed range throwing. 6 tests become 15.

Packages, re-queried on NuGet on 2026-10-04 (newest stable that day):

  • Microsoft.NET.Test.Sdk 16.9.4 to 18.10.1
  • MSTest.TestAdapter 2.2.3 to 4.4.1
  • MSTest.TestFramework 2.2.3 to 4.4.1
  • coverlet.collector 3.0.2 to 10.1.0

Local run on SDK 10.0.302, runtime 10.0.10: build 0 warnings 0 errors, dotnet test 15 passed 0 failed, dotnet run exits 0, dotnet list package --vulnerable --include-transitive reports nothing.

- Retarget SliceArrays and SliceArrayTests from net6.0 to net10.0.
- Fix the crash on dotnet run: the reversed range array[3..1] now sits in its
  own method inside try/catch, so the program runs to completion.
- Align array[..2] with the article and the test assertion with it.
- Top-level statements instead of namespace, class Program and Main.
- Add AsSpan, Span<T>.Slice, index from end, List<T> slicing (range,
  GetRange, CollectionsMarshal.AsSpan) and string slicing, with tests.
- Bump test packages.
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