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
Conversation
…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.
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.
Retargets
collections-lists/HowToCloneAListfrom 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. ItsClone()returnedMemberwiseClone(), which copiesList<T>'s private backing array by reference, so the "clone" shared storage with the original: writingclone[0]changedoriginal[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 aGetRangeclone. 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
ConverAlltypo in the console output,Pizza.NameandPizza.Toppingsmaderequiredwith[SetsRequiredMembers]on the copy constructor (without the attribute the copy-constructor call fails with CS9035), andFirstin place ofFirstOrDefaultwhere 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.