From 6874b87f1353c30bb24cd70cf8f7a8ed7968b482 Mon Sep 17 00:00:00 2001 From: Nick Craver Date: Tue, 3 Nov 2015 07:11:43 -0500 Subject: [PATCH] C#6 Cleanup & Optimizations The main intent is cleanup using C# 6 operators and normalization of type aliases. While there are no intended functional changes here, it does eliminate a few tight race conditions as a bonus (not a real-win since this isn't thread-safe all over, simply noting). --- .../IFormCollection.cs | 1 - .../PathString.cs | 3 +-- .../QueryString.cs | 6 +++--- .../RequestHeaders.cs | 1 - src/Microsoft.AspNet.Http/FormCollection.cs | 6 +----- src/Microsoft.AspNet.Http/HeaderDictionary.cs | 19 +++---------------- .../HttpContextAccessor.cs | 2 +- .../OwinFeatureCollection.cs | 12 ++++-------- .../WebSockets/OwinWebSocketAcceptAdapter.cs | 2 +- .../WebEncoders.cs | 2 +- .../BaseHeaderParser.cs | 2 +- .../CacheControlHeaderValue.cs | 2 +- .../HttpHeaderParser.cs | 7 +++---- .../StringWithQualityHeaderValueComparer.cs | 6 +++--- .../CookieHeaderValueTest.cs | 2 +- .../SetCookieHeaderValueTest.cs | 2 +- 16 files changed, 25 insertions(+), 50 deletions(-) diff --git a/src/Microsoft.AspNet.Http.Abstractions/IFormCollection.cs b/src/Microsoft.AspNet.Http.Abstractions/IFormCollection.cs index 03e8116852..55d4a578bc 100644 --- a/src/Microsoft.AspNet.Http.Abstractions/IFormCollection.cs +++ b/src/Microsoft.AspNet.Http.Abstractions/IFormCollection.cs @@ -88,7 +88,6 @@ namespace Microsoft.AspNet.Http /// /// The file collection sent with the request. /// - /// /// The files included with the request. IFormFileCollection Files { get; } } diff --git a/src/Microsoft.AspNet.Http.Abstractions/PathString.cs b/src/Microsoft.AspNet.Http.Abstractions/PathString.cs index 8a76d8f548..ef6e1bb39e 100644 --- a/src/Microsoft.AspNet.Http.Abstractions/PathString.cs +++ b/src/Microsoft.AspNet.Http.Abstractions/PathString.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Linq; using System.Text.Encodings.Web; namespace Microsoft.AspNet.Http @@ -17,7 +16,7 @@ namespace Microsoft.AspNet.Http /// /// Represents the empty path. This field is read-only. /// - public static readonly PathString Empty = new PathString(String.Empty); + public static readonly PathString Empty = new PathString(string.Empty); private readonly string _value; diff --git a/src/Microsoft.AspNet.Http.Abstractions/QueryString.cs b/src/Microsoft.AspNet.Http.Abstractions/QueryString.cs index 088402b2a7..d400375ea8 100644 --- a/src/Microsoft.AspNet.Http.Abstractions/QueryString.cs +++ b/src/Microsoft.AspNet.Http.Abstractions/QueryString.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Http /// /// Represents the empty query string. This field is read-only. /// - public static readonly QueryString Empty = new QueryString(String.Empty); + public static readonly QueryString Empty = new QueryString(string.Empty); private readonly string _value; @@ -84,7 +84,7 @@ namespace Microsoft.AspNet.Http [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1057:StringUriOverloadsCallSystemUriOverloads", Justification = "Delimiter characters ? and # must be escaped by this method instead of truncating the value")] public static QueryString FromUriComponent(string uriComponent) { - if (String.IsNullOrEmpty(uriComponent)) + if (string.IsNullOrEmpty(uriComponent)) { return new QueryString(string.Empty); } @@ -231,7 +231,7 @@ namespace Microsoft.AspNet.Http public override int GetHashCode() { - return (_value != null ? _value.GetHashCode() : 0); + return _value?.GetHashCode() ?? 0; } public static bool operator ==(QueryString left, QueryString right) diff --git a/src/Microsoft.AspNet.Http.Extensions/RequestHeaders.cs b/src/Microsoft.AspNet.Http.Extensions/RequestHeaders.cs index dbfff55411..59265fbf08 100644 --- a/src/Microsoft.AspNet.Http.Extensions/RequestHeaders.cs +++ b/src/Microsoft.AspNet.Http.Extensions/RequestHeaders.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using Microsoft.AspNet.Http.Extensions; using Microsoft.Net.Http.Headers; namespace Microsoft.AspNet.Http.Headers diff --git a/src/Microsoft.AspNet.Http/FormCollection.cs b/src/Microsoft.AspNet.Http/FormCollection.cs index 7a7d67c26d..fbc3b88a38 100644 --- a/src/Microsoft.AspNet.Http/FormCollection.cs +++ b/src/Microsoft.AspNet.Http/FormCollection.cs @@ -83,11 +83,7 @@ namespace Microsoft.AspNet.Http.Internal { get { - if (Store == null) - { - return 0; - } - return Store.Count; + return Store?.Count ?? 0; } } diff --git a/src/Microsoft.AspNet.Http/HeaderDictionary.cs b/src/Microsoft.AspNet.Http/HeaderDictionary.cs index f1fde19a1d..530c5f31e0 100644 --- a/src/Microsoft.AspNet.Http/HeaderDictionary.cs +++ b/src/Microsoft.AspNet.Http/HeaderDictionary.cs @@ -72,12 +72,7 @@ namespace Microsoft.AspNet.Http.Internal if (StringValues.IsNullOrEmpty(value)) { - if (Store == null) - { - return; - } - - Store.Remove(key); + Store?.Remove(key); } else { @@ -110,11 +105,7 @@ namespace Microsoft.AspNet.Http.Internal { get { - if (Store == null) - { - return 0; - } - return Store.Count; + return Store?.Count ?? 0; } } @@ -195,11 +186,7 @@ namespace Microsoft.AspNet.Http.Internal /// public void Clear() { - if (Store == null) - { - return; - } - Store.Clear(); + Store?.Clear(); } /// diff --git a/src/Microsoft.AspNet.Http/HttpContextAccessor.cs b/src/Microsoft.AspNet.Http/HttpContextAccessor.cs index 83b558161d..04784d714a 100644 --- a/src/Microsoft.AspNet.Http/HttpContextAccessor.cs +++ b/src/Microsoft.AspNet.Http/HttpContextAccessor.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNet.Http.Internal get { var handle = CallContext.LogicalGetData(LogicalDataKey) as ObjectHandle; - return handle != null ? handle.Unwrap() as HttpContext : null; + return handle?.Unwrap() as HttpContext; } set { diff --git a/src/Microsoft.AspNet.Owin/OwinFeatureCollection.cs b/src/Microsoft.AspNet.Owin/OwinFeatureCollection.cs index 6d955194fa..acf3e2215c 100644 --- a/src/Microsoft.AspNet.Owin/OwinFeatureCollection.cs +++ b/src/Microsoft.AspNet.Owin/OwinFeatureCollection.cs @@ -17,7 +17,6 @@ using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features.Authentication; -using Microsoft.Extensions.Primitives; namespace Microsoft.AspNet.Owin { @@ -45,14 +44,11 @@ namespace Microsoft.AspNet.Owin SupportsWebSockets = true; var register = Prop, object>>(OwinConstants.CommonKeys.OnSendingHeaders); - if (register != null) + register?.Invoke(state => { - register(state => - { - var collection = (OwinFeatureCollection)state; - collection._headersSent = true; - }, this); - } + var collection = (OwinFeatureCollection)state; + collection._headersSent = true; + }, this); } T Prop(string key) diff --git a/src/Microsoft.AspNet.Owin/WebSockets/OwinWebSocketAcceptAdapter.cs b/src/Microsoft.AspNet.Owin/WebSockets/OwinWebSocketAcceptAdapter.cs index a36e9377f3..87b3cf6ffa 100644 --- a/src/Microsoft.AspNet.Owin/WebSockets/OwinWebSocketAcceptAdapter.cs +++ b/src/Microsoft.AspNet.Owin/WebSockets/OwinWebSocketAcceptAdapter.cs @@ -57,7 +57,7 @@ namespace Microsoft.AspNet.Owin options = acceptContext.Options; _subProtocol = acceptContext.SubProtocol; } - else if (context != null && context.SubProtocol != null) + else if (context?.SubProtocol != null) { options = new Dictionary(1) { diff --git a/src/Microsoft.AspNet.WebUtilities/WebEncoders.cs b/src/Microsoft.AspNet.WebUtilities/WebEncoders.cs index 751ad43c21..4142039f8f 100644 --- a/src/Microsoft.AspNet.WebUtilities/WebEncoders.cs +++ b/src/Microsoft.AspNet.WebUtilities/WebEncoders.cs @@ -121,7 +121,7 @@ namespace Microsoft.AspNet.WebUtilities // Special-case empty input if (count == 0) { - return String.Empty; + return string.Empty; } // We're going to use base64url encoding with no padding characters. diff --git a/src/Microsoft.Net.Http.Headers/BaseHeaderParser.cs b/src/Microsoft.Net.Http.Headers/BaseHeaderParser.cs index dbd18b82d6..1b4038d21b 100644 --- a/src/Microsoft.Net.Http.Headers/BaseHeaderParser.cs +++ b/src/Microsoft.Net.Http.Headers/BaseHeaderParser.cs @@ -44,7 +44,7 @@ namespace Microsoft.Net.Http.Headers return SupportsMultipleValues; } - T result = default(T); + T result; var length = GetParsedValueLength(value, current, out result); if (length == 0) diff --git a/src/Microsoft.Net.Http.Headers/CacheControlHeaderValue.cs b/src/Microsoft.Net.Http.Headers/CacheControlHeaderValue.cs index 94793efbea..7ad65ac3bc 100644 --- a/src/Microsoft.Net.Http.Headers/CacheControlHeaderValue.cs +++ b/src/Microsoft.Net.Http.Headers/CacheControlHeaderValue.cs @@ -369,7 +369,7 @@ namespace Microsoft.Net.Http.Headers return 0; } - nameValueList.Add(nameValue as NameValueHeaderValue); + nameValueList.Add(nameValue); } // If we get here, we were able to successfully parse the string as list of name/value pairs. Now analyze diff --git a/src/Microsoft.Net.Http.Headers/HttpHeaderParser.cs b/src/Microsoft.Net.Http.Headers/HttpHeaderParser.cs index 2c05296a98..a12edbf3e0 100644 --- a/src/Microsoft.Net.Http.Headers/HttpHeaderParser.cs +++ b/src/Microsoft.Net.Http.Headers/HttpHeaderParser.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Globalization; @@ -37,11 +36,11 @@ namespace Microsoft.Net.Http.Headers // If a parser returns 'null', it means there was no value, but that's valid (e.g. "Accept: "). The caller // can ignore the value. - T result = default(T); + T result; if (!TryParseValue(value, ref index, out result)) { throw new FormatException(string.Format(CultureInfo.InvariantCulture, "Invalid value '{0}'.", - value == null ? "" : value.Substring(index))); + value?.Substring(index) ?? "")); } return result; } @@ -114,7 +113,7 @@ namespace Microsoft.Net.Http.Headers else { throw new FormatException(string.Format(CultureInfo.InvariantCulture, "Invalid values '{0}'.", - values == null ? "" : value.Substring(index))); + value.Substring(index))); } } } diff --git a/src/Microsoft.Net.Http.Headers/StringWithQualityHeaderValueComparer.cs b/src/Microsoft.Net.Http.Headers/StringWithQualityHeaderValueComparer.cs index 6dc3d0758a..0d092ec19d 100644 --- a/src/Microsoft.Net.Http.Headers/StringWithQualityHeaderValueComparer.cs +++ b/src/Microsoft.Net.Http.Headers/StringWithQualityHeaderValueComparer.cs @@ -64,13 +64,13 @@ namespace Microsoft.Net.Http.Headers return 1; } - if (!String.Equals(stringWithQuality1.Value, stringWithQuality2.Value, StringComparison.OrdinalIgnoreCase)) + if (!string.Equals(stringWithQuality1.Value, stringWithQuality2.Value, StringComparison.OrdinalIgnoreCase)) { - if (String.Equals(stringWithQuality1.Value, "*", StringComparison.Ordinal)) + if (string.Equals(stringWithQuality1.Value, "*", StringComparison.Ordinal)) { return -1; } - else if (String.Equals(stringWithQuality2.Value, "*", StringComparison.Ordinal)) + else if (string.Equals(stringWithQuality2.Value, "*", StringComparison.Ordinal)) { return 1; } diff --git a/test/Microsoft.Net.Http.Headers.Tests/CookieHeaderValueTest.cs b/test/Microsoft.Net.Http.Headers.Tests/CookieHeaderValueTest.cs index f649cce09e..3644ed1df0 100644 --- a/test/Microsoft.Net.Http.Headers.Tests/CookieHeaderValueTest.cs +++ b/test/Microsoft.Net.Http.Headers.Tests/CookieHeaderValueTest.cs @@ -154,7 +154,7 @@ namespace Microsoft.Net.Http.Headers public void CookieHeaderValue_Value() { var cookie = new CookieHeaderValue("name"); - Assert.Equal(String.Empty, cookie.Value); + Assert.Equal(string.Empty, cookie.Value); cookie.Value = "value1"; Assert.Equal("value1", cookie.Value); diff --git a/test/Microsoft.Net.Http.Headers.Tests/SetCookieHeaderValueTest.cs b/test/Microsoft.Net.Http.Headers.Tests/SetCookieHeaderValueTest.cs index e21fb4e089..f98886521d 100644 --- a/test/Microsoft.Net.Http.Headers.Tests/SetCookieHeaderValueTest.cs +++ b/test/Microsoft.Net.Http.Headers.Tests/SetCookieHeaderValueTest.cs @@ -184,7 +184,7 @@ namespace Microsoft.Net.Http.Headers public void SetCookieHeaderValue_Value() { var cookie = new SetCookieHeaderValue("name"); - Assert.Equal(String.Empty, cookie.Value); + Assert.Equal(string.Empty, cookie.Value); cookie.Value = "value1"; Assert.Equal("value1", cookie.Value);