Dictionary.GetValueOrDefault (#8641)
authorAlexander Radchenko <radchenkosasha@gmail.com>
Fri, 16 Dec 2016 21:06:41 +0000 (04:06 +0700)
committerIan Hays <ianha@microsoft.com>
Fri, 16 Dec 2016 21:06:41 +0000 (13:06 -0800)
* Dictionary.GetValueOrDefault

* Fixed IDictionary.TryGetValue

* remove extensions

* // Method similar to TryGetValue that returns the value instead of putting it in an out param.

* public TValue GetValueOrDefault(TKey key) => GetValueOrDefault(key, default(TValue));

src/mscorlib/model.xml
src/mscorlib/src/System/Collections/Generic/Dictionary.cs

index 31b00eb867f80f503bccb8bcda910c34d5cb46b2..c9bf5dd8adcd85450d53d867d552ea6af5753f82 100644 (file)
       <Member Name="get_Keys" />
       <Member Name="get_Values" />
       <Member Name="GetEnumerator" />
+      <Member Name="GetValueOrDefault(TKey)" />
+      <Member Name="GetValueOrDefault(TKey,TValue)" />
       <Member Name="Remove(TKey)" />
       <Member Name="set_Item(TKey,TValue)" />
       <Member Name="TryGetValue(TKey,TValue@)" />
index dc5053098d0840a10ed858c3112656168001d6b6..c2b2da9ad24a9c03a3b856a4d773c7785d5422f5 100644 (file)
@@ -523,16 +523,19 @@ namespace System.Collections.Generic {
             return false;
         }
 
-        // This is a convenience method for the internal callers that were converted from using Hashtable.
-        // Many were combining key doesn't exist and key exists but null value (for non-value types) checks.
-        // This allows them to continue getting that behavior with minimal code delta. This is basically
-        // TryGetValue without the out param
-        internal TValue GetValueOrDefault(TKey key) {
+        // Method similar to TryGetValue that returns the value instead of putting it in an out param.
+        public TValue GetValueOrDefault(TKey key) => GetValueOrDefault(key, default(TValue));
+
+        // Method similar to TryGetValue that returns the value instead of putting it in an out param. If the entry
+        // doesn't exist, returns the defaultValue instead.
+        public TValue GetValueOrDefault(TKey key, TValue defaultValue)
+        {
             int i = FindEntry(key);
-            if (i >= 0) {
+            if (i >= 0)
+            {
                 return entries[i].value;
             }
-            return default(TValue);
+            return defaultValue;
         }
 
         bool ICollection<KeyValuePair<TKey,TValue>>.IsReadOnly {