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
Expand Up @@ -51,14 +51,23 @@ public static SortedList<int, string> AddElementsToSortedList(SortedList<int, st

public static SortedList<int, string> RemoveElementsFromSortedList(SortedList<int, string> myGenericList)
{
myGenericList.RemoveAt(3); // removes (2, "") which is the item at position 3
myGenericList.Remove(4); // removes (4, "")
myGenericList.RemoveAt(1); // removes the element with key 2, which sits at index 1
myGenericList.Remove(4); // removes the element with key 4

myGenericList.Clear(); // removes all the elements and returns an empty collection
myGenericList.Clear(); // removes all the elements, Count is now 0

return myGenericList;
}

public static (string Third, string Fifth, int FirstKey) ReadElementsFromSortedList(SortedList<int, string> myGenericList)
{
var third = myGenericList[3]; // "This is my third value"
myGenericList.TryGetValue(5, out var fifth); // false, fifth is null
var firstKey = myGenericList.GetKeyAtIndex(0); // 1

return (third, fifth, firstKey);
}

public static bool CheckContentsOfGenericList(SortedList<int, string> myGenericList, int myKey)
{
return myGenericList.ContainsKey(myKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace SortedListInCSharp;

public static class MemoryComparison
{
public static long RetainedBytes(Func<object> fill)
{
var before = GC.GetTotalMemory(forceFullCollection: true);
var collection = fill();
var after = GC.GetTotalMemory(forceFullCollection: true);
GC.KeepAlive(collection);

return after - before;
}

public static IDictionary<int, int> Fill(IDictionary<int, int> collection, int[] keys)
{
foreach (var key in keys)
{
collection.Add(key, key);
}

return collection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static SortedList NonGenericSortedList()
myNonGenericList.Add(2, "This is my second string element");
// These next lines will give us runtime exceptions...
/*
myNonGenericList.Add("three", "This is my third string element"); // System.ArgumentException: 'Item has already been added. Key in dictionary: '1' Key being added: 'three''
myNonGenericList.Add("three", "This is my third string element"); // System.InvalidOperationException: Failed to compare two elements in the array.
myNonGenericList.Add(null, "There is nothing to see here"); // System.ArgumentNullException: 'Key cannot be null. (Parameter 'key')'
*/

Expand Down
32 changes: 21 additions & 11 deletions collections-csharp/SortedListInCSharp/SortedListInCSharp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
using System;
using SortedListInCSharp;

namespace SortedListInCSharp
var keys = Enumerable.Range(0, 100_000).ToArray();

var sortedList = MemoryComparison.RetainedBytes(
() => MemoryComparison.Fill(new SortedList<int, int>(), keys));
var sortedDictionary = MemoryComparison.RetainedBytes(
() => MemoryComparison.Fill(new SortedDictionary<int, int>(), keys));
var dictionary = MemoryComparison.RetainedBytes(
() => MemoryComparison.Fill(new Dictionary<int, int>(), keys));
var trimmedSortedList = MemoryComparison.RetainedBytes(() =>
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}

}
var list = new SortedList<int, int>();
MemoryComparison.Fill(list, keys);
list.TrimExcess();

return list;
});

Console.WriteLine($"SortedList: {sortedList:N0} bytes");
Console.WriteLine($"SortedDictionary: {sortedDictionary:N0} bytes");
Console.WriteLine($"Dictionary: {dictionary:N0} bytes");
Console.WriteLine($"SortedList after TrimExcess(): {trimmedSortedList:N0} bytes");
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
10 changes: 5 additions & 5 deletions collections-csharp/SortedListInCSharp/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
30 changes: 26 additions & 4 deletions collections-csharp/SortedListInCSharp/Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void whenGenericSortedListIsCalled_MethodProducesCorrectListType()
var genericSortedList = GenericLists.GenericSortedList();
var expected = typeof(SortedList<int, string>);
var output = genericSortedList.GetType();
Assert.AreEqual(output, expected);
Assert.AreEqual(expected, output);
}

[TestMethod]
Expand All @@ -26,7 +26,7 @@ public void whenNonGenericSortedListIsCalled_MethodProducesCorrectListType()
var genericSortedList = NonGenericLists.NonGenericSortedList();
var expected = typeof(SortedList);
var output = genericSortedList.GetType();
Assert.AreEqual(output, expected);
Assert.AreEqual(expected, output);
}

[TestMethod]
Expand All @@ -35,7 +35,7 @@ public void whenGenericSortedListFromDictionaryIsCalled_MethodProducesCorrectLis
var genericSortedList = GenericLists.GenericSortedListFromDictionary();
var expected = typeof(SortedList<int, string>);
var output = genericSortedList.GetType();
Assert.AreEqual(output, expected);
Assert.AreEqual(expected, output);
}

[TestMethod]
Expand Down Expand Up @@ -66,7 +66,7 @@ public void whenRemoveElementsFromSortedListIsCalled_MethodProducesCorrectListCo

GenericLists.RemoveElementsFromSortedList(testGenericSortedList);

Assert.AreEqual(testGenericSortedList.Count, 0);
Assert.AreEqual(0, testGenericSortedList.Count);
}

[TestMethod]
Expand Down Expand Up @@ -100,5 +100,27 @@ public void whenCheckContentsOfNonGenericListIsCalledWithValue_MethodMustReturnA
testNonGenericSortedList.Add(4, "This is my fourth string element");
Assert.IsFalse(NonGenericLists.CheckContentsOfNonGenericList(testNonGenericSortedList, "This is my first string element"));
}

[TestMethod]
public void whenReadElementsFromSortedListIsCalled_MethodReturnsValuesByKeyAndIndex()
{
testGenericSortedList = new SortedList<int, string>();
GenericLists.AddElementsToSortedList(testGenericSortedList);

var (third, fifth, firstKey) = GenericLists.ReadElementsFromSortedList(testGenericSortedList);

Assert.AreEqual("This is my third value", third);
Assert.IsNull(fifth);
Assert.AreEqual(1, firstKey);
}

[TestMethod]
public void whenNonGenericSortedListGetsKeyOfAnotherType_AddThrowsInvalidOperationException()
{
var nonGenericSortedList = NonGenericLists.NonGenericSortedList();

Assert.ThrowsExactly<System.InvalidOperationException>(
() => nonGenericSortedList.Add("three", "This is my third string element"));
}
}
}
Loading