Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="FluentAssertions" Version="7.2.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FluentAssertions;
using FluentAssertions;

namespace HowToCloneAList.Tests
{
Expand Down Expand Up @@ -60,5 +60,29 @@ public void GivenAValidPizza_WhenToStringMethodIsInvoked_ThenToStringMethodMetho

expectedOutput.Should().Be(margherita.ToString());
}

[Fact]
public void GivenAListOfPizzas_WhenProjectedThroughTheCopyConstructor_ThenTheCloneKeepsItsToppings()
{
var pizzas = new List<Pizza>
{
new Pizza
{
Name = "Margherita",
Toppings = new List<string>
{
"Mozzarella",
"Olive oil",
"Basil"
}
}
};

List<Pizza> clone = [.. pizzas.Select(p => new Pizza(p))];

pizzas[0].Toppings.Clear();

clone[0].Toppings.Should().HaveCount(3);
}
}
}
}
22 changes: 22 additions & 0 deletions collections-lists/HowToCloneAList/HowToCloneAList.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,27 @@ public void GivenAValidList_WhenConvertAllMethodIsInvoked_ThenConvertAllMethodRe

listClone.Should().BeEquivalentTo(list);
}

[Fact]
public void GivenAValidList_WhenACollectionExpressionIsUsed_ThenTheCollectionExpressionReturnsNewListInstance()
{
var list = new List<string> { "one", "two", "three" };

List<string> listClone = [.. list];

listClone.Should().BeEquivalentTo(list);
listClone.Should().NotBeSameAs(list);
}

[Fact]
public void GivenAValidList_WhenGetRangeMethodIsInvoked_ThenGetRangeMethodReturnsNewListInstance()
{
var list = new List<string> { "one", "two", "three" };

var listClone = list.GetRange(0, list.Count);

listClone.Should().BeEquivalentTo(list);
listClone.Should().NotBeSameAs(list);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
9 changes: 6 additions & 3 deletions collections-lists/HowToCloneAList/HowToCloneAList/Pizza.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
namespace HowToCloneAList
using System.Diagnostics.CodeAnalysis;

namespace HowToCloneAList
{
public class Pizza : ICloneable
{
public Pizza()
{
}

[SetsRequiredMembers]
public Pizza(Pizza pizza)
{
Name = pizza.Name;
Toppings = pizza.Toppings.ToList();
}

public string Name { get; set; }
public List<string> Toppings { get; set; }
public required string Name { get; set; }
public required List<string> Toppings { get; set; }

public object Clone()
{
Expand Down
23 changes: 7 additions & 16 deletions collections-lists/HowToCloneAList/HowToCloneAList/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,18 @@ static void Main(string[] args)
var toppingsClonedWithConvertAll = toppings
.ConvertAll(new Converter<string, string>(x => x));

var customToppingsList = new ToppingsList<string>
{
"Mozzarella",
"Olive oil",
"Basil"
};
List<string> toppingsClonedWithCollectionExpression = [.. toppings];

var toppingsClonedWithICloneable = (ToppingsList<string>)customToppingsList.Clone();
var toppingsClonedWithGetRange = toppings.GetRange(0, toppings.Count);

Console.WriteLine("Original list: " + string.Join(", ", toppings));
Console.WriteLine("Cloned with Constructor: " + string.Join(", ", toppingsClonedWithConstructor));
Console.WriteLine("Cloned with CopyTo: " + string.Join(", ", toppingsClonedWithCopyTo));
Console.WriteLine("Cloned with AddRange: " + string.Join(", ", toppingsClonedWithAddRange));
Console.WriteLine("Cloned with ToList: " + string.Join(", ", toppingsClonedWithToList));
Console.WriteLine("Cloned with ConverAll: " + string.Join(", ", toppingsClonedWithConvertAll));
Console.WriteLine("Cloned with ICloneable: " + string.Join(", ", toppingsClonedWithICloneable));
Console.WriteLine("Cloned with ConvertAll: " + string.Join(", ", toppingsClonedWithConvertAll));
Console.WriteLine("Cloned with a collection expression: " + string.Join(", ", toppingsClonedWithCollectionExpression));
Console.WriteLine("Cloned with GetRange: " + string.Join(", ", toppingsClonedWithGetRange));

var pizzas = new List<Pizza>
{
Expand Down Expand Up @@ -74,15 +70,10 @@ static void Main(string[] args)
pizzasClonedWithICloneable.Add((Pizza)pizza.Clone());
}

var pizzasClonedWithCopyConstructor = new List<Pizza>();

foreach (var pizza in pizzas)
{
pizzasClonedWithCopyConstructor.Add(new Pizza(pizza));
}
List<Pizza> pizzasClonedWithCopyConstructor = [.. pizzas.Select(p => new Pizza(p))];

var margherita = pizzas
.FirstOrDefault(x => x.Name == "Margherita");
.First(x => x.Name == "Margherita");

margherita.Toppings.Clear();

Expand Down
10 changes: 0 additions & 10 deletions collections-lists/HowToCloneAList/HowToCloneAList/ToppingsList.cs

This file was deleted.

Loading