diff --git a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp.Benchmark/DefaultValueFromDictionaryInCSharp.Benchmark.csproj b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp.Benchmark/DefaultValueFromDictionaryInCSharp.Benchmark.csproj
index 04f03a0973..62be4f867f 100644
--- a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp.Benchmark/DefaultValueFromDictionaryInCSharp.Benchmark.csproj
+++ b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp.Benchmark/DefaultValueFromDictionaryInCSharp.Benchmark.csproj
@@ -2,13 +2,13 @@
Exe
- net7.0
+ net10.0
enable
enable
-
+
diff --git a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp.csproj b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp.csproj
index 4071f55782..dfb40caafc 100644
--- a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp.csproj
+++ b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp.csproj
@@ -2,14 +2,9 @@
Exe
- net7.0
+ net10.0
enable
enable
-
-
-
-
-
diff --git a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodContainsKey.cs b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodContainsKey.cs
index 676ba2b8a5..daf2f06cb5 100644
--- a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodContainsKey.cs
+++ b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodContainsKey.cs
@@ -2,7 +2,7 @@
{
public static class MethodContainsKey
{
- public static dynamic GetValueFromDictionary(Dictionary dictionary, string key)
+ public static T? GetValueFromDictionary(Dictionary dictionary, string key)
{
return dictionary.ContainsKey(key) ? dictionary[key] : default;
}
diff --git a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodGetValueOrDefault.cs b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodGetValueOrDefault.cs
index 580a7c4072..77e6f4dc0c 100644
--- a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodGetValueOrDefault.cs
+++ b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodGetValueOrDefault.cs
@@ -2,7 +2,7 @@
{
public static class MethodGetValueOrDefault
{
- public static dynamic GetValueFromDictionary(Dictionary dictionary, string? key)
+ public static T? GetValueFromDictionary(Dictionary dictionary, string key)
{
return dictionary.GetValueOrDefault(key);
}
diff --git a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodGetValueOrDefaultWithFallback.cs b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodGetValueOrDefaultWithFallback.cs
new file mode 100644
index 0000000000..5ea2fd22c5
--- /dev/null
+++ b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodGetValueOrDefaultWithFallback.cs
@@ -0,0 +1,10 @@
+namespace DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefaultWithFallback
+{
+ public static class MethodGetValueOrDefaultWithFallback
+ {
+ public static T GetValueFromDictionary(Dictionary dictionary, string key, T fallback)
+ {
+ return dictionary.GetValueOrDefault(key, fallback);
+ }
+ }
+}
diff --git a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodTryGetValue.cs b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodTryGetValue.cs
index 2c9b210886..a70a878d9c 100644
--- a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodTryGetValue.cs
+++ b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Methods/MethodTryGetValue.cs
@@ -2,7 +2,7 @@
{
public static class MethodTryGetValue
{
- public static dynamic? GetValueFromDictionary(Dictionary dictionary, string key)
+ public static T? GetValueFromDictionary(Dictionary dictionary, string key)
{
return dictionary.TryGetValue(key, out var value) ? value : default;
}
diff --git a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Program.cs b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Program.cs
index a2cc90c2b5..453090710b 100644
--- a/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Program.cs
+++ b/collections-dictionary/DefaultValueFromDictionaryInCSharp/DefaultValueFromDictionaryInCSharp/Program.cs
@@ -1,5 +1,6 @@
using DefaultValueFromDictionaryInCSharp.MethodContainsKey;
using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefault;
+using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefaultWithFallback;
using DefaultValueFromDictionaryInCSharp.MethodTryGetValue;
var myDictionary = new Dictionary
@@ -16,4 +17,6 @@
Console.WriteLine($"{MethodGetValueOrDefault.GetValueFromDictionary(myDictionary, key)}");
+Console.WriteLine($"{MethodGetValueOrDefaultWithFallback.GetValueFromDictionary(myDictionary, key, -1)}");
+
Console.ReadKey();
\ No newline at end of file
diff --git a/collections-dictionary/DefaultValueFromDictionaryInCSharp/Tests/DefaultValueFromDictionaryUnitTests.cs b/collections-dictionary/DefaultValueFromDictionaryInCSharp/Tests/DefaultValueFromDictionaryUnitTests.cs
index 8fed336f7b..7f7df6fae1 100644
--- a/collections-dictionary/DefaultValueFromDictionaryInCSharp/Tests/DefaultValueFromDictionaryUnitTests.cs
+++ b/collections-dictionary/DefaultValueFromDictionaryInCSharp/Tests/DefaultValueFromDictionaryUnitTests.cs
@@ -1,4 +1,7 @@
+using DefaultValueFromDictionaryInCSharp.MethodContainsKey;
using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefault;
+using DefaultValueFromDictionaryInCSharp.MethodGetValueOrDefaultWithFallback;
+using DefaultValueFromDictionaryInCSharp.MethodTryGetValue;
namespace Tests
{
@@ -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]
@@ -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 { { "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), pair);
+ Assert.IsNull(pair.Key);
+ Assert.AreEqual(0, pair.Value);
}
}
-}
\ No newline at end of file
+}
diff --git a/collections-dictionary/DefaultValueFromDictionaryInCSharp/Tests/Tests.csproj b/collections-dictionary/DefaultValueFromDictionaryInCSharp/Tests/Tests.csproj
index d9bf2f63ea..3b65724ff8 100644
--- a/collections-dictionary/DefaultValueFromDictionaryInCSharp/Tests/Tests.csproj
+++ b/collections-dictionary/DefaultValueFromDictionaryInCSharp/Tests/Tests.csproj
@@ -1,7 +1,7 @@
- net7.0
+ net10.0
enable
enable
@@ -9,10 +9,10 @@
-
-
-
-
+
+
+
+