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 @@ -2,13 +2,13 @@

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

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

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

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.4" />
<PackageReference Include="System.Collections" Version="4.3.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public static class MethodContainsKey
{
public static dynamic GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key)
public static T? GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key)
{
return dictionary.ContainsKey(key) ? dictionary[key] : default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public static class MethodGetValueOrDefault
{
public static dynamic GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string? key)
public static T? GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key)
{
return dictionary.GetValueOrDefault(key);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefaultWithFallback
{
public static class MethodGetValueOrDefaultWithFallback
{
public static T GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key, T fallback)
{
return dictionary.GetValueOrDefault(key, fallback);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public static class MethodTryGetValue
{
public static dynamic? GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key)
public static T? GetValueFromDictionary<T>(Dictionary<string, T> dictionary, string key)
{
return dictionary.TryGetValue(key, out var value) ? value : default;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DefaultValueFromDictionaryInCSharp.MethodContainsKey;
using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefault;
using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefaultWithFallback;
using DefaultValueFromDictionaryInCSharp.MethodTryGetValue;

var myDictionary = new Dictionary<string, int>
Expand All @@ -16,4 +17,6 @@

Console.WriteLine($"{MethodGetValueOrDefault.GetValueFromDictionary(myDictionary, key)}");

Console.WriteLine($"{MethodGetValueOrDefaultWithFallback.GetValueFromDictionary(myDictionary, key, -1)}");

Console.ReadKey();
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using DefaultValueFromDictionaryInCSharp.MethodContainsKey;
using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefault;
using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefaultWithFallback;
using DefaultValueFromDictionaryInCSharp.MethodTryGetValue;

namespace Tests
{
Expand All @@ -17,7 +20,7 @@ public void GivenMethodGetValueOrDefault_WhenKeyNotExisting_ThenDefaultValueZero
var key = "sam";
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);

Assert.AreEqual(value, 0);
Assert.AreEqual(0, value);
}

[TestMethod]
Expand All @@ -26,43 +29,77 @@ public void GivenMethodGetValueOrDefault_WhenKeyBob_ThenValueTwo()
var key = "bob";
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);

Assert.AreEqual(value, 2);
Assert.AreEqual(2, value);
}

[TestMethod]
public void GivenMethodTryGetValue_WhenKeyNotExisting_ThenDefaultValueZero()
{
var key = "sam";
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);
var value = MethodTryGetValue.GetValueFromDictionary(_myDictionary, key);

Assert.AreEqual(value, 0);
Assert.AreEqual(0, value);
}

[TestMethod]
public void GivenMethodTryGetValue_WhenKeyBob_ThenValueTwo()
{
var key = "bob";
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);
var value = MethodTryGetValue.GetValueFromDictionary(_myDictionary, key);

Assert.AreEqual(value, 2);
Assert.AreEqual(2, value);
}

[TestMethod]
public void GivenMethodContainsKey_WhenKeyNotExisting_ThenDefaultValueZero()
{
var key = "sam";
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);
var value = MethodContainsKey.GetValueFromDictionary(_myDictionary, key);

Assert.AreEqual(value, 0);
Assert.AreEqual(0, value);
}

[TestMethod]
public void GivenMethodContainsKey_WhenKeyBob_ThenValueTwo()
{
var key = "bob";
var value = MethodGetValueOrDefault.GetValueFromDictionary(_myDictionary, key);
var value = MethodContainsKey.GetValueFromDictionary(_myDictionary, key);

Assert.AreEqual(2, value);
}

[TestMethod]
public void GivenMethodGetValueOrDefaultWithFallback_WhenKeyNotExisting_ThenFallbackValue()
{
var key = "sam";
var value = MethodGetValueOrDefaultWithFallback.GetValueFromDictionary(_myDictionary, key, -1);

Assert.AreEqual(-1, value);
}

[TestMethod]
public void GivenMethodGetValueOrDefaultWithFallback_WhenStoredValueEqualsFallback_ThenSameResultAsMissingKey()
{
var dictionary = new Dictionary<string, int> { { "neg", -1 } };

var stored = MethodGetValueOrDefaultWithFallback.GetValueFromDictionary(dictionary, "neg", -1);
var missing = MethodGetValueOrDefaultWithFallback.GetValueFromDictionary(dictionary, "gone", -1);

Assert.AreEqual(-1, stored);
Assert.AreEqual(-1, missing);
Assert.AreEqual(stored, missing);
}

[TestMethod]
public void GivenFirstOrDefault_WhenKeyNotExisting_ThenDefaultKeyValuePairNotNull()
{
var searchKey = "sam";

var pair = _myDictionary.FirstOrDefault(p => p.Key == searchKey);

Assert.AreEqual(value, 2);
Assert.AreEqual(default(KeyValuePair<string, int>), pair);
Assert.IsNull(pair.Key);
Assert.AreEqual(0, pair.Value);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<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="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.3.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.3.3" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading