Skip to content

How to clone a list: retarget net10.0, collection expression and GetRange, remove broken ToppingsList clone - #2210

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/81588-csharp-how-to-clone-a-list
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/81588-csharp-how-to-clone-a-list

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Retargets collections-lists/HowToCloneAList from net7.0 to net10.0 and updates the test stack: xunit 2.9.3, xunit.runner.visualstudio 4.0.0, Microsoft.NET.Test.Sdk 18.10.1, coverlet.collector 10.0.1, FluentAssertions 6.9.0 to 7.2.2 (the newest Apache-2.0 release; the 8.x line moved to a community licence).

Removes ToppingsList<T> and its test. Its Clone() returned MemberwiseClone(), which copies List<T>'s private backing array by reference, so the "clone" shared storage with the original: writing clone[0] changed original[0]. The single test on it only asserted equivalence, so the defect never failed anything.

Adds the two forms the sample was missing, each with a test: a collection-expression clone (List<string> clone = [.. toppings];) and a GetRange clone. Builds the copy-constructor deep copy as a one-line projection and adds a test asserting the projected clone still has its three toppings after the original's are cleared.

Smaller fixes: the ConverAll typo in the console output, Pizza.Name and Pizza.Toppings made required with [SetsRequiredMembers] on the copy constructor (without the attribute the copy-constructor call fails with CS9035), and First in place of FirstOrDefault where the element is known to exist.

The folder previously built with three nullable warnings (CS8618 twice, CS8602 once). It now builds with 0 warnings and 0 errors and runs 11 of 11 tests on SDK 10.0.302 / runtime 10.0.10.

…ange, remove broken ToppingsList clone

Move both projects from net7.0 to net10.0 and update the test stack (xunit 2.9.3, runner 4.0.0, Test.Sdk 18.10.1, coverlet 10.0.1, FluentAssertions 7.2.2).

Remove ToppingsList<T> and its test: its Clone() called MemberwiseClone(), which copies the private backing array by reference, so the clone shared storage with the original and was not a clone at all.

Add a collection-expression clone and a GetRange clone with tests, build the copy-constructor deep copy as a one-line projection, and add a test proving the projected clone keeps its toppings after the original is cleared.

Fix the ConverAll typo in the console output, make Pizza.Name and Pizza.Toppings required with [SetsRequiredMembers] on the copy constructor, and use First instead of FirstOrDefault. The folder now builds with 0 warnings and runs 11 of 11 tests.
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