You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2020/02/03 16:39:00 UTC
[lucenenet] 05/08: Lucene.Net.Support.DictionaryExtensions:
Optimized Put() method, added guard clauses to Put and PutAll
This is an automated email from the ASF dual-hosted git repository.
nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 89c413433621703a349f4d9f6d19871605794d43
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Mon Feb 3 21:47:51 2020 +0700
Lucene.Net.Support.DictionaryExtensions: Optimized Put() method, added guard clauses to Put and PutAll
---
src/Lucene.Net/Support/DictionaryExtensions.cs | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/src/Lucene.Net/Support/DictionaryExtensions.cs b/src/Lucene.Net/Support/DictionaryExtensions.cs
index 09c9bd5..ea67408 100644
--- a/src/Lucene.Net/Support/DictionaryExtensions.cs
+++ b/src/Lucene.Net/Support/DictionaryExtensions.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
-using System.IO;
namespace Lucene.Net.Support
{
@@ -25,6 +24,9 @@ namespace Lucene.Net.Support
{
public static void PutAll<TKey, TValue>(this IDictionary<TKey, TValue> dict, IEnumerable<KeyValuePair<TKey, TValue>> kvps)
{
+ if (dict == null)
+ throw new ArgumentNullException(nameof(dict));
+
foreach (var kvp in kvps)
{
dict[kvp.Key] = kvp.Value;
@@ -34,9 +36,10 @@ namespace Lucene.Net.Support
public static TValue Put<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue value)
{
if (dict == null)
- return default(TValue);
+ throw new ArgumentNullException(nameof(dict));
- var oldValue = dict.ContainsKey(key) ? dict[key] : default(TValue);
+ if (!dict.TryGetValue(key, out TValue oldValue))
+ oldValue = default;
dict[key] = value;
return oldValue;
}