diff --git a/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs b/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs index 841cfa2642..bc0b7a26ce 100644 --- a/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs +++ b/src/Microsoft.AspNetCore.Http/HeaderDictionary.cs @@ -32,11 +32,19 @@ namespace Microsoft.AspNetCore.Http public HeaderDictionary(int capacity) { - Store = new Dictionary(capacity, StringComparer.OrdinalIgnoreCase); + EnsureStore(capacity); } private Dictionary Store { get; set; } + private void EnsureStore(int capacity) + { + if (Store == null) + { + Store = new Dictionary(capacity, StringComparer.OrdinalIgnoreCase); + } + } + /// /// Get or sets the associated value from the collection as a single string. /// @@ -72,11 +80,7 @@ namespace Microsoft.AspNetCore.Http } else { - if (Store == null) - { - Store = new Dictionary(1, StringComparer.OrdinalIgnoreCase); - } - + EnsureStore(1); Store[key] = value; } } @@ -173,10 +177,7 @@ namespace Microsoft.AspNetCore.Http throw new ArgumentNullException("The key is null"); } ThrowIfReadOnly(); - if (Store == null) - { - Store = new Dictionary(1, StringComparer.OrdinalIgnoreCase); - } + EnsureStore(1); Store.Add(item.Key, item.Value); } @@ -192,11 +193,7 @@ namespace Microsoft.AspNetCore.Http throw new ArgumentNullException(nameof(key)); } ThrowIfReadOnly(); - - if (Store == null) - { - Store = new Dictionary(1); - } + EnsureStore(1); Store.Add(key, value); }