diff options
author | Ben Adams <thundercat@illyriad.co.uk> | 2019-01-06 02:49:58 +0100 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2019-01-05 17:49:58 -0800 |
commit | d0cdf8813ef099deec3b6940e34f20d1bba054b8 (patch) | |
tree | bd0f4eb03f4a5f09e3c9970c320988e80c076c3f /src/System.Private.CoreLib/shared | |
parent | 110835b1b818b333f27ab76db3f223a03027698a (diff) | |
download | coreclr-d0cdf8813ef099deec3b6940e34f20d1bba054b8.tar.gz coreclr-d0cdf8813ef099deec3b6940e34f20d1bba054b8.tar.bz2 coreclr-d0cdf8813ef099deec3b6940e34f20d1bba054b8.zip |
(C#7) Use pattern matching `is` rather than `as` with null check (#21828)
* Use pattern matching `is` rather than `as` with null check
Diffstat (limited to 'src/System.Private.CoreLib/shared')
41 files changed, 86 insertions, 194 deletions
diff --git a/src/System.Private.CoreLib/shared/System/AggregateException.cs b/src/System.Private.CoreLib/shared/System/AggregateException.cs index 022a1eae39..048005c479 100644 --- a/src/System.Private.CoreLib/shared/System/AggregateException.cs +++ b/src/System.Private.CoreLib/shared/System/AggregateException.cs @@ -399,11 +399,9 @@ namespace System continue; } - AggregateException currentInnerAsAggregate = currentInnerException as AggregateException; - // If this exception is an aggregate, keep it around for later. Otherwise, // simply add it to the list of flattened exceptions to be returned. - if (currentInnerAsAggregate != null) + if (currentInnerException is AggregateException currentInnerAsAggregate) { exceptionsToFlatten.Add(currentInnerAsAggregate); } diff --git a/src/System.Private.CoreLib/shared/System/AppContext.cs b/src/System.Private.CoreLib/shared/System/AppContext.cs index 9df7c8dc8e..c3994ea351 100644 --- a/src/System.Private.CoreLib/shared/System/AppContext.cs +++ b/src/System.Private.CoreLib/shared/System/AppContext.cs @@ -99,8 +99,7 @@ namespace System } } - string value = GetData(switchName) as string; - if (value != null && bool.TryParse(value, out isEnabled)) + if (GetData(switchName) is string value && bool.TryParse(value, out isEnabled)) { return true; } diff --git a/src/System.Private.CoreLib/shared/System/Collections/Comparer.cs b/src/System.Private.CoreLib/shared/System/Collections/Comparer.cs index e22fb5bb91..24a6cbff62 100644 --- a/src/System.Private.CoreLib/shared/System/Collections/Comparer.cs +++ b/src/System.Private.CoreLib/shared/System/Collections/Comparer.cs @@ -59,16 +59,13 @@ namespace System.Collections if (b == null) return 1; string sa = a as string; - string sb = b as string; - if (sa != null && sb != null) + if (sa != null && b is string sb) return _compareInfo.Compare(sa, sb); - IComparable ia = a as IComparable; - if (ia != null) + if (a is IComparable ia) return ia.CompareTo(b); - IComparable ib = b as IComparable; - if (ib != null) + if (b is IComparable ib) return -ib.CompareTo(a); throw new ArgumentException(SR.Argument_ImplementIComparable); diff --git a/src/System.Private.CoreLib/shared/System/Collections/CompatibleComparer.cs b/src/System.Private.CoreLib/shared/System/Collections/CompatibleComparer.cs index 587fd68391..5342dcd6fb 100644 --- a/src/System.Private.CoreLib/shared/System/Collections/CompatibleComparer.cs +++ b/src/System.Private.CoreLib/shared/System/Collections/CompatibleComparer.cs @@ -37,8 +37,7 @@ namespace System.Collections return _comparer.Compare(a, b); } - IComparable ia = a as IComparable; - if (ia != null) + if (a is IComparable ia) { return ia.CompareTo(b); } diff --git a/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueue.cs b/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueue.cs index f3634e8864..17e7fd5b77 100644 --- a/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueue.cs +++ b/src/System.Private.CoreLib/shared/System/Collections/Concurrent/ConcurrentQueue.cs @@ -80,8 +80,7 @@ namespace System.Collections.Concurrent // case we round its length up to a power of 2, as all segments must // be a power of 2 in length. int length = InitialSegmentLength; - var c = collection as ICollection<T>; - if (c != null) + if (collection is ICollection<T> c) { int count = c.Count; if (count > length) @@ -143,8 +142,7 @@ namespace System.Collections.Concurrent void ICollection.CopyTo(Array array, int index) { // Special-case when the Array is actually a T[], taking a faster path - T[] szArray = array as T[]; - if (szArray != null) + if (array is T[] szArray) { CopyTo(szArray, index); return; diff --git a/src/System.Private.CoreLib/shared/System/Collections/ObjectModel/Collection.cs b/src/System.Private.CoreLib/shared/System/Collections/ObjectModel/Collection.cs index 9efd671275..c96a1576f3 100644 --- a/src/System.Private.CoreLib/shared/System/Collections/ObjectModel/Collection.cs +++ b/src/System.Private.CoreLib/shared/System/Collections/ObjectModel/Collection.cs @@ -215,8 +215,7 @@ namespace System.Collections.ObjectModel ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall); } - T[] tArray = array as T[]; - if (tArray != null) + if (array is T[] tArray) { items.CopyTo(tArray, index); } @@ -294,8 +293,7 @@ namespace System.Collections.ObjectModel // readonly collections are fixed size, if our internal item // collection does not implement IList. Note that Array implements // IList, and therefore T[] and U[] will be fixed-size. - IList list = items as IList; - if (list != null) + if (items is IList list) { return list.IsFixedSize; } diff --git a/src/System.Private.CoreLib/shared/System/Collections/ObjectModel/ReadOnlyCollection.cs b/src/System.Private.CoreLib/shared/System/Collections/ObjectModel/ReadOnlyCollection.cs index 584b4cc711..9d463da751 100644 --- a/src/System.Private.CoreLib/shared/System/Collections/ObjectModel/ReadOnlyCollection.cs +++ b/src/System.Private.CoreLib/shared/System/Collections/ObjectModel/ReadOnlyCollection.cs @@ -147,8 +147,7 @@ namespace System.Collections.ObjectModel ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall); } - T[] items = array as T[]; - if (items != null) + if (array is T[] items) { list.CopyTo(items, index); } diff --git a/src/System.Private.CoreLib/shared/System/ComponentModel/DefaultValueAttribute.cs b/src/System.Private.CoreLib/shared/System/ComponentModel/DefaultValueAttribute.cs index 5f7108a527..d506ffae4c 100644 --- a/src/System.Private.CoreLib/shared/System/ComponentModel/DefaultValueAttribute.cs +++ b/src/System.Private.CoreLib/shared/System/ComponentModel/DefaultValueAttribute.cs @@ -225,9 +225,7 @@ namespace System.ComponentModel return true; } - DefaultValueAttribute other = obj as DefaultValueAttribute; - - if (other != null) + if (obj is DefaultValueAttribute other) { if (Value != null) { diff --git a/src/System.Private.CoreLib/shared/System/ComponentModel/EditorBrowsableAttribute.cs b/src/System.Private.CoreLib/shared/System/ComponentModel/EditorBrowsableAttribute.cs index 9b4d6e626e..a59ee839bc 100644 --- a/src/System.Private.CoreLib/shared/System/ComponentModel/EditorBrowsableAttribute.cs +++ b/src/System.Private.CoreLib/shared/System/ComponentModel/EditorBrowsableAttribute.cs @@ -28,9 +28,7 @@ namespace System.ComponentModel return true; } - EditorBrowsableAttribute other = obj as EditorBrowsableAttribute; - - return (other != null) && other.browsableState == browsableState; + return (obj is EditorBrowsableAttribute other) && other.browsableState == browsableState; } public override int GetHashCode() diff --git a/src/System.Private.CoreLib/shared/System/Convert.cs b/src/System.Private.CoreLib/shared/System/Convert.cs index 077dda9d4f..2bc01794e4 100644 --- a/src/System.Private.CoreLib/shared/System/Convert.cs +++ b/src/System.Private.CoreLib/shared/System/Convert.cs @@ -160,8 +160,7 @@ namespace System public static TypeCode GetTypeCode(object value) { if (value == null) return TypeCode.Empty; - IConvertible temp = value as IConvertible; - if (temp != null) + if (value is IConvertible temp) { return temp.GetTypeCode(); } @@ -173,8 +172,7 @@ namespace System public static bool IsDBNull(object value) { if (value == System.DBNull.Value) return true; - IConvertible convertible = value as IConvertible; - return convertible != null ? convertible.GetTypeCode() == TypeCode.DBNull : false; + return value is IConvertible convertible ? convertible.GetTypeCode() == TypeCode.DBNull : false; } // Converts the given object to the given type. In general, this method is @@ -201,8 +199,7 @@ namespace System return null; } - IConvertible v = value as IConvertible; - if (v == null) + if (!(value is IConvertible v)) { throw new InvalidCastException(SR.InvalidCast_IConvertible); } @@ -330,8 +327,7 @@ namespace System return null; } - IConvertible ic = value as IConvertible; - if (ic == null) + if (!(value is IConvertible ic)) { if (value.GetType() == conversionType) { @@ -2002,11 +1998,9 @@ namespace System public static string ToString(object value, IFormatProvider provider) { - IConvertible ic = value as IConvertible; - if (ic != null) + if (value is IConvertible ic) return ic.ToString(provider); - IFormattable formattable = value as IFormattable; - if (formattable != null) + if (value is IFormattable formattable) return formattable.ToString(null, provider); return value == null ? string.Empty : value.ToString(); } diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs index 3dc1cb050b..8159bad6b2 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs @@ -403,8 +403,7 @@ namespace System.Diagnostics.Tracing { foreach (WeakReference eventSourceRef in EventListener.s_EventSources) { - EventSource eventSource = eventSourceRef.Target as EventSource; - if (eventSource != null && !eventSource.IsDisposed) + if (eventSourceRef.Target is EventSource eventSource && !eventSource.IsDisposed) ret.Add(eventSource); } } @@ -2715,8 +2714,7 @@ namespace System.Diagnostics.Tracing // TODO Enforce singleton pattern foreach (WeakReference eventSourceRef in EventListener.s_EventSources) { - EventSource eventSource = eventSourceRef.Target as EventSource; - if (eventSource != null && eventSource.Guid == m_guid && !eventSource.IsDisposed) + if (eventSourceRef.Target is EventSource eventSource && eventSource.Guid == m_guid && !eventSource.IsDisposed) { if (eventSource != this) { @@ -4160,8 +4158,7 @@ namespace System.Diagnostics.Tracing { foreach (var esRef in s_EventSources) { - EventSource es = esRef.Target as EventSource; - if (es != null) + if (esRef.Target is EventSource es) es.Dispose(); } } @@ -4181,8 +4178,7 @@ namespace System.Diagnostics.Tracing // Foreach existing EventSource in the appdomain foreach (WeakReference eventSourceRef in s_EventSources) { - EventSource eventSource = eventSourceRef.Target as EventSource; - if (eventSource != null) + if (eventSourceRef.Target is EventSource eventSource) { // Is the first output dispatcher the dispatcher we are removing? if (eventSource.m_Dispatchers.m_Listener == listenerToRemove) @@ -4246,8 +4242,7 @@ namespace System.Diagnostics.Tracing foreach (WeakReference eventSourceRef in s_EventSources) { id++; - EventSource eventSource = eventSourceRef.Target as EventSource; - if (eventSource == null) + if (!(eventSourceRef.Target is EventSource eventSource)) continue; Debug.Assert(eventSource.m_id == id, "Unexpected event source ID."); @@ -4328,8 +4323,7 @@ namespace System.Diagnostics.Tracing for (int i = 0; i < eventSourcesSnapshot.Length; i++) { WeakReference eventSourceRef = eventSourcesSnapshot[i]; - EventSource eventSource = eventSourceRef.Target as EventSource; - if (eventSource != null) + if (eventSourceRef.Target is EventSource eventSource) { EventSourceCreatedEventArgs args = new EventSourceCreatedEventArgs(); args.EventSource = eventSource; diff --git a/src/System.Private.CoreLib/shared/System/Globalization/CompareInfo.cs b/src/System.Private.CoreLib/shared/System/Globalization/CompareInfo.cs index 21fd7212a8..9f7669edcc 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/CompareInfo.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/CompareInfo.cs @@ -1371,9 +1371,7 @@ namespace System.Globalization public override bool Equals(object value) { - CompareInfo that = value as CompareInfo; - - if (that != null) + if (value is CompareInfo that) { return this.Name == that.Name; } diff --git a/src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.cs b/src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.cs index f3f56dda3e..153512639a 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/CultureInfo.cs @@ -872,9 +872,7 @@ namespace System.Globalization if (object.ReferenceEquals(this, value)) return true; - CultureInfo that = value as CultureInfo; - - if (that != null) + if (value is CultureInfo that) { // using CompareInfo to verify the data passed through the constructor // CultureInfo(String cultureName, String textAndCompareCultureName) diff --git a/src/System.Private.CoreLib/shared/System/Globalization/IdnMapping.cs b/src/System.Private.CoreLib/shared/System/Globalization/IdnMapping.cs index 849602abcb..56f38ff628 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/IdnMapping.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/IdnMapping.cs @@ -145,9 +145,8 @@ namespace System.Globalization public override bool Equals(object obj) { - IdnMapping that = obj as IdnMapping; return - that != null && + obj is IdnMapping that && _allowUnassigned == that._allowUnassigned && _useStd3AsciiRules == that._useStd3AsciiRules; } diff --git a/src/System.Private.CoreLib/shared/System/Globalization/RegionInfo.cs b/src/System.Private.CoreLib/shared/System/Globalization/RegionInfo.cs index 8416257d91..576672b222 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/RegionInfo.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/RegionInfo.cs @@ -360,8 +360,7 @@ namespace System.Globalization //////////////////////////////////////////////////////////////////////// public override bool Equals(object value) { - RegionInfo that = value as RegionInfo; - if (that != null) + if (value is RegionInfo that) { return this.Name.Equals(that.Name); } diff --git a/src/System.Private.CoreLib/shared/System/Globalization/SortKey.cs b/src/System.Private.CoreLib/shared/System/Globalization/SortKey.cs index 9624946257..f9c7f68f5e 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/SortKey.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/SortKey.cs @@ -135,9 +135,7 @@ namespace System.Globalization //////////////////////////////////////////////////////////////////////// public override bool Equals(object value) { - SortKey that = value as SortKey; - - if (that != null) + if (value is SortKey that) { return Compare(this, that) == 0; } diff --git a/src/System.Private.CoreLib/shared/System/Globalization/StringInfo.cs b/src/System.Private.CoreLib/shared/System/Globalization/StringInfo.cs index 936a3cb9c1..425e6f2379 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/StringInfo.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/StringInfo.cs @@ -34,8 +34,7 @@ namespace System.Globalization public override bool Equals(object value) { - StringInfo that = value as StringInfo; - if (that != null) + if (value is StringInfo that) { return (_str.Equals(that._str)); } diff --git a/src/System.Private.CoreLib/shared/System/Globalization/TextInfo.cs b/src/System.Private.CoreLib/shared/System/Globalization/TextInfo.cs index 61e2160a62..77964fb642 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/TextInfo.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/TextInfo.cs @@ -654,9 +654,7 @@ namespace System.Globalization //////////////////////////////////////////////////////////////////////// public override bool Equals(object obj) { - TextInfo that = obj as TextInfo; - - if (that != null) + if (obj is TextInfo that) { return CultureName.Equals(that.CultureName); } diff --git a/src/System.Private.CoreLib/shared/System/IO/FileStreamCompletionSource.Win32.cs b/src/System.Private.CoreLib/shared/System/IO/FileStreamCompletionSource.Win32.cs index ec499d8456..a7996fd042 100644 --- a/src/System.Private.CoreLib/shared/System/IO/FileStreamCompletionSource.Win32.cs +++ b/src/System.Private.CoreLib/shared/System/IO/FileStreamCompletionSource.Win32.cs @@ -137,8 +137,7 @@ namespace System.IO // be directly the FileStreamCompletion that's completing (in the case where the preallocated // overlapped was already in use by another operation). object state = ThreadPoolBoundHandle.GetNativeOverlappedState(pOverlapped); - FileStream fs = state as FileStream; - FileStreamCompletionSource completionSource = fs != null ? + FileStreamCompletionSource completionSource = state is FileStream fs ? fs._currentOverlappedOwner : (FileStreamCompletionSource)state; Debug.Assert(completionSource._overlapped == pOverlapped, "Overlaps don't match"); diff --git a/src/System.Private.CoreLib/shared/System/IO/MemoryStream.cs b/src/System.Private.CoreLib/shared/System/IO/MemoryStream.cs index 8ca162b57c..b9f2b5e35f 100644 --- a/src/System.Private.CoreLib/shared/System/IO/MemoryStream.cs +++ b/src/System.Private.CoreLib/shared/System/IO/MemoryStream.cs @@ -527,8 +527,7 @@ namespace System.IO return Task.CompletedTask; // If destination is not a memory stream, write there asynchronously: - MemoryStream memStrDest = destination as MemoryStream; - if (memStrDest == null) + if (!(destination is MemoryStream memStrDest)) return destination.WriteAsync(_buffer, pos, n, cancellationToken); try diff --git a/src/System.Private.CoreLib/shared/System/IO/Stream.cs b/src/System.Private.CoreLib/shared/System/IO/Stream.cs index fe9f9fe24e..537d28ca8a 100644 --- a/src/System.Private.CoreLib/shared/System/IO/Stream.cs +++ b/src/System.Private.CoreLib/shared/System/IO/Stream.cs @@ -1078,8 +1078,7 @@ namespace System.IO internal static int EndRead(IAsyncResult asyncResult) { - SynchronousAsyncResult ar = asyncResult as SynchronousAsyncResult; - if (ar == null || ar._isWrite) + if (!(asyncResult is SynchronousAsyncResult ar) || ar._isWrite) throw new ArgumentException(SR.Arg_WrongAsyncResult); if (ar._endXxxCalled) @@ -1093,8 +1092,7 @@ namespace System.IO internal static void EndWrite(IAsyncResult asyncResult) { - SynchronousAsyncResult ar = asyncResult as SynchronousAsyncResult; - if (ar == null || !ar._isWrite) + if (!(asyncResult is SynchronousAsyncResult ar) || !ar._isWrite) throw new ArgumentException(SR.Arg_WrongAsyncResult); if (ar._endXxxCalled) diff --git a/src/System.Private.CoreLib/shared/System/IO/TextWriter.cs b/src/System.Private.CoreLib/shared/System/IO/TextWriter.cs index e470517a7b..030a015e3c 100644 --- a/src/System.Private.CoreLib/shared/System/IO/TextWriter.cs +++ b/src/System.Private.CoreLib/shared/System/IO/TextWriter.cs @@ -286,8 +286,7 @@ namespace System.IO { if (value != null) { - IFormattable f = value as IFormattable; - if (f != null) + if (value is IFormattable f) { Write(f.ToString(null, FormatProvider)); } @@ -497,8 +496,7 @@ namespace System.IO { // Call WriteLine(value.ToString), not Write(Object), WriteLine(). // This makes calls to WriteLine(Object) atomic. - IFormattable f = value as IFormattable; - if (f != null) + if (value is IFormattable f) { WriteLine(f.ToString(null, FormatProvider)); } diff --git a/src/System.Private.CoreLib/shared/System/String.Comparison.cs b/src/System.Private.CoreLib/shared/System/String.Comparison.cs index 360167df40..5bae67be0e 100644 --- a/src/System.Private.CoreLib/shared/System/String.Comparison.cs +++ b/src/System.Private.CoreLib/shared/System/String.Comparison.cs @@ -511,9 +511,7 @@ namespace System return 1; } - string other = value as string; - - if (other == null) + if (!(value is string other)) { throw new ArgumentException(SR.Arg_MustBeString); } @@ -606,8 +604,7 @@ namespace System if (object.ReferenceEquals(this, obj)) return true; - string str = obj as string; - if (str == null) + if (!(obj is string str)) return false; if (this.Length != str.Length) diff --git a/src/System.Private.CoreLib/shared/System/StringComparer.cs b/src/System.Private.CoreLib/shared/System/StringComparer.cs index e4378c5efa..8fab833592 100644 --- a/src/System.Private.CoreLib/shared/System/StringComparer.cs +++ b/src/System.Private.CoreLib/shared/System/StringComparer.cs @@ -114,18 +114,15 @@ namespace System if (x == null) return -1; if (y == null) return 1; - string sa = x as string; - if (sa != null) + if (x is string sa) { - string sb = y as string; - if (sb != null) + if (y is string sb) { return Compare(sa, sb); } } - IComparable ia = x as IComparable; - if (ia != null) + if (x is IComparable ia) { return ia.CompareTo(y); } @@ -138,11 +135,9 @@ namespace System if (x == y) return true; if (x == null || y == null) return false; - string sa = x as string; - if (sa != null) + if (x is string sa) { - string sb = y as string; - if (sb != null) + if (y is string sb) { return Equals(sa, sb); } @@ -157,8 +152,7 @@ namespace System throw new ArgumentNullException(nameof(obj)); } - string s = obj as string; - if (s != null) + if (obj is string s) { return GetHashCode(s); } @@ -232,9 +226,8 @@ namespace System // Equals method for the comparer itself. public override bool Equals(object obj) { - CultureAwareComparer comparer = obj as CultureAwareComparer; return - comparer != null && + obj is CultureAwareComparer comparer && _options == comparer._options && _compareInfo.Equals(comparer._compareInfo); } @@ -316,8 +309,7 @@ namespace System // Equals method for the comparer itself. public override bool Equals(object obj) { - OrdinalComparer comparer = obj as OrdinalComparer; - if (comparer == null) + if (!(obj is OrdinalComparer comparer)) { return false; } diff --git a/src/System.Private.CoreLib/shared/System/Text/DecoderBestFitFallback.cs b/src/System.Private.CoreLib/shared/System/Text/DecoderBestFitFallback.cs index 8c62730c26..d94554c6fd 100644 --- a/src/System.Private.CoreLib/shared/System/Text/DecoderBestFitFallback.cs +++ b/src/System.Private.CoreLib/shared/System/Text/DecoderBestFitFallback.cs @@ -40,8 +40,7 @@ namespace System.Text public override bool Equals(object value) { - InternalDecoderBestFitFallback that = value as InternalDecoderBestFitFallback; - if (that != null) + if (value is InternalDecoderBestFitFallback that) { return (_encoding.CodePage == that._encoding.CodePage); } diff --git a/src/System.Private.CoreLib/shared/System/Text/DecoderExceptionFallback.cs b/src/System.Private.CoreLib/shared/System/Text/DecoderExceptionFallback.cs index 56c004714f..0525861a5b 100644 --- a/src/System.Private.CoreLib/shared/System/Text/DecoderExceptionFallback.cs +++ b/src/System.Private.CoreLib/shared/System/Text/DecoderExceptionFallback.cs @@ -31,8 +31,7 @@ namespace System.Text public override bool Equals(object value) { - DecoderExceptionFallback that = value as DecoderExceptionFallback; - if (that != null) + if (value is DecoderExceptionFallback that) { return (true); } diff --git a/src/System.Private.CoreLib/shared/System/Text/DecoderReplacementFallback.cs b/src/System.Private.CoreLib/shared/System/Text/DecoderReplacementFallback.cs index a97baf05cb..2af0d12535 100644 --- a/src/System.Private.CoreLib/shared/System/Text/DecoderReplacementFallback.cs +++ b/src/System.Private.CoreLib/shared/System/Text/DecoderReplacementFallback.cs @@ -84,8 +84,7 @@ namespace System.Text public override bool Equals(object value) { - DecoderReplacementFallback that = value as DecoderReplacementFallback; - if (that != null) + if (value is DecoderReplacementFallback that) { return (_strDefault == that._strDefault); } diff --git a/src/System.Private.CoreLib/shared/System/Text/EncoderBestFitFallback.cs b/src/System.Private.CoreLib/shared/System/Text/EncoderBestFitFallback.cs index 4aab3f62a3..c3d07c980e 100644 --- a/src/System.Private.CoreLib/shared/System/Text/EncoderBestFitFallback.cs +++ b/src/System.Private.CoreLib/shared/System/Text/EncoderBestFitFallback.cs @@ -40,8 +40,7 @@ namespace System.Text public override bool Equals(object value) { - InternalEncoderBestFitFallback that = value as InternalEncoderBestFitFallback; - if (that != null) + if (value is InternalEncoderBestFitFallback that) { return (_encoding.CodePage == that._encoding.CodePage); } diff --git a/src/System.Private.CoreLib/shared/System/Text/EncoderExceptionFallback.cs b/src/System.Private.CoreLib/shared/System/Text/EncoderExceptionFallback.cs index 92afcf701a..077d92762f 100644 --- a/src/System.Private.CoreLib/shared/System/Text/EncoderExceptionFallback.cs +++ b/src/System.Private.CoreLib/shared/System/Text/EncoderExceptionFallback.cs @@ -30,8 +30,7 @@ namespace System.Text public override bool Equals(object value) { - EncoderExceptionFallback that = value as EncoderExceptionFallback; - if (that != null) + if (value is EncoderExceptionFallback that) { return (true); } diff --git a/src/System.Private.CoreLib/shared/System/Text/EncoderReplacementFallback.cs b/src/System.Private.CoreLib/shared/System/Text/EncoderReplacementFallback.cs index 760c280fde..cfce4108f5 100644 --- a/src/System.Private.CoreLib/shared/System/Text/EncoderReplacementFallback.cs +++ b/src/System.Private.CoreLib/shared/System/Text/EncoderReplacementFallback.cs @@ -87,8 +87,7 @@ namespace System.Text public override bool Equals(object value) { - EncoderReplacementFallback that = value as EncoderReplacementFallback; - if (that != null) + if (value is EncoderReplacementFallback that) { return (_strDefault == that._strDefault); } diff --git a/src/System.Private.CoreLib/shared/System/Text/Encoding.cs b/src/System.Private.CoreLib/shared/System/Text/Encoding.cs index c1b64961f7..175e5442fd 100644 --- a/src/System.Private.CoreLib/shared/System/Text/Encoding.cs +++ b/src/System.Private.CoreLib/shared/System/Text/Encoding.cs @@ -1190,8 +1190,7 @@ namespace System.Text public override bool Equals(object value) { - Encoding that = value as Encoding; - if (that != null) + if (value is Encoding that) return (_codePage == that._codePage) && (EncoderFallback.Equals(that.EncoderFallback)) && (DecoderFallback.Equals(that.DecoderFallback)); diff --git a/src/System.Private.CoreLib/shared/System/Text/EncodingInfo.cs b/src/System.Private.CoreLib/shared/System/Text/EncodingInfo.cs index b8d634c22e..002d9ef0b5 100644 --- a/src/System.Private.CoreLib/shared/System/Text/EncodingInfo.cs +++ b/src/System.Private.CoreLib/shared/System/Text/EncodingInfo.cs @@ -24,8 +24,7 @@ namespace System.Text public override bool Equals(object value) { - EncodingInfo that = value as EncodingInfo; - if (that != null) + if (value is EncodingInfo that) { return this.CodePage == that.CodePage; } diff --git a/src/System.Private.CoreLib/shared/System/Text/UTF32Encoding.cs b/src/System.Private.CoreLib/shared/System/Text/UTF32Encoding.cs index d579cc9e5e..02f3167df2 100644 --- a/src/System.Private.CoreLib/shared/System/Text/UTF32Encoding.cs +++ b/src/System.Private.CoreLib/shared/System/Text/UTF32Encoding.cs @@ -1159,8 +1159,7 @@ namespace System.Text public override bool Equals(object value) { - UTF32Encoding that = value as UTF32Encoding; - if (that != null) + if (value is UTF32Encoding that) { return (_emitUTF32ByteOrderMark == that._emitUTF32ByteOrderMark) && (_bigEndian == that._bigEndian) && diff --git a/src/System.Private.CoreLib/shared/System/Text/UTF7Encoding.cs b/src/System.Private.CoreLib/shared/System/Text/UTF7Encoding.cs index 5c51c81076..a932778b45 100644 --- a/src/System.Private.CoreLib/shared/System/Text/UTF7Encoding.cs +++ b/src/System.Private.CoreLib/shared/System/Text/UTF7Encoding.cs @@ -99,8 +99,7 @@ namespace System.Text public override bool Equals(object value) { - UTF7Encoding that = value as UTF7Encoding; - if (that != null) + if (value is UTF7Encoding that) { return (_allowOptionals == that._allowOptionals) && (EncoderFallback.Equals(that.EncoderFallback)) && diff --git a/src/System.Private.CoreLib/shared/System/Text/UTF8Encoding.cs b/src/System.Private.CoreLib/shared/System/Text/UTF8Encoding.cs index 426835ed53..5e1b9ae9a3 100644 --- a/src/System.Private.CoreLib/shared/System/Text/UTF8Encoding.cs +++ b/src/System.Private.CoreLib/shared/System/Text/UTF8Encoding.cs @@ -2555,8 +2555,7 @@ namespace System.Text public override bool Equals(object value) { - UTF8Encoding that = value as UTF8Encoding; - if (that != null) + if (value is UTF8Encoding that) { return (_emitUTF8Identifier == that._emitUTF8Identifier) && (EncoderFallback.Equals(that.EncoderFallback)) && diff --git a/src/System.Private.CoreLib/shared/System/Text/UnicodeEncoding.cs b/src/System.Private.CoreLib/shared/System/Text/UnicodeEncoding.cs index d80c229693..870962c6b6 100644 --- a/src/System.Private.CoreLib/shared/System/Text/UnicodeEncoding.cs +++ b/src/System.Private.CoreLib/shared/System/Text/UnicodeEncoding.cs @@ -1842,8 +1842,7 @@ namespace System.Text public override bool Equals(object value) { - UnicodeEncoding that = value as UnicodeEncoding; - if (that != null) + if (value is UnicodeEncoding that) { // // Big Endian Unicode has different code page (1201) than small Endian one (1200), diff --git a/src/System.Private.CoreLib/shared/System/Threading/Tasks/Future.cs b/src/System.Private.CoreLib/shared/System/Threading/Tasks/Future.cs index e3339efd42..5cc0b6737b 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Tasks/Future.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Tasks/Future.cs @@ -597,14 +597,13 @@ namespace System.Threading.Tasks { // Invoke the delegate Debug.Assert(m_action != null); - var func = m_action as Func<TResult>; - if (func != null) + if (m_action is Func<TResult> func) { m_result = func(); return; } - var funcWithState = m_action as Func<object, TResult>; - if (funcWithState != null) + + if (m_action is Func<object, TResult> funcWithState) { m_result = funcWithState(m_stateObject); return; diff --git a/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskExceptionHolder.cs b/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskExceptionHolder.cs index 0037923bdb..cdf5c78ea4 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskExceptionHolder.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskExceptionHolder.cs @@ -123,8 +123,7 @@ namespace System.Threading.Tasks // If this changes, make sure to only conditionally mark as handled below. // Store the cancellation exception - var oce = exceptionObject as OperationCanceledException; - if (oce != null) + if (exceptionObject is OperationCanceledException oce) { m_cancellationException = ExceptionDispatchInfo.Capture(oce); } @@ -155,24 +154,21 @@ namespace System.Threading.Tasks else Debug.Assert(exceptions.Count > 0, "Expected existing exceptions list to have > 0 exceptions."); // Handle Exception by capturing it into an ExceptionDispatchInfo and storing that - var exception = exceptionObject as Exception; - if (exception != null) + if (exceptionObject is Exception exception) { exceptions.Add(ExceptionDispatchInfo.Capture(exception)); } else { // Handle ExceptionDispatchInfo by storing it into the list - var edi = exceptionObject as ExceptionDispatchInfo; - if (edi != null) + if (exceptionObject is ExceptionDispatchInfo edi) { exceptions.Add(edi); } else { // Handle enumerables of exceptions by capturing each of the contained exceptions into an EDI and storing it - var exColl = exceptionObject as IEnumerable<Exception>; - if (exColl != null) + if (exceptionObject is IEnumerable<Exception> exColl) { #if DEBUG int numExceptions = 0; @@ -192,8 +188,7 @@ namespace System.Threading.Tasks else { // Handle enumerables of EDIs by storing them directly - var ediColl = exceptionObject as IEnumerable<ExceptionDispatchInfo>; - if (ediColl != null) + if (exceptionObject is IEnumerable<ExceptionDispatchInfo> ediColl) { exceptions.AddRange(ediColl); #if DEBUG diff --git a/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskScheduler.cs b/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskScheduler.cs index c5bf02b9bc..13257b3077 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskScheduler.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Tasks/TaskScheduler.cs @@ -500,8 +500,7 @@ namespace System.Threading.Tasks return null; // If it can be cast to an array, use it directly - Task[] activeTasksArray = activeTasksSource as Task[]; - if (activeTasksArray == null) + if (!(activeTasksSource is Task[] activeTasksArray)) { activeTasksArray = (new List<Task>(activeTasksSource)).ToArray(); } diff --git a/src/System.Private.CoreLib/shared/System/TimeZoneInfo.Win32.cs b/src/System.Private.CoreLib/shared/System/TimeZoneInfo.Win32.cs index a16f6a8764..a7095dee61 100644 --- a/src/System.Private.CoreLib/shared/System/TimeZoneInfo.Win32.cs +++ b/src/System.Private.CoreLib/shared/System/TimeZoneInfo.Win32.cs @@ -678,8 +678,7 @@ namespace System private static unsafe bool TryGetTimeZoneEntryFromRegistry(RegistryKey key, string name, out REG_TZI_FORMAT dtzi) { - byte[] regValue = key.GetValue(name, null) as byte[]; - if (regValue == null || regValue.Length != sizeof(REG_TZI_FORMAT)) + if (!(key.GetValue(name, null) is byte[] regValue) || regValue.Length != sizeof(REG_TZI_FORMAT)) { dtzi = default; return false; diff --git a/src/System.Private.CoreLib/shared/System/Tuple.cs b/src/System.Private.CoreLib/shared/System/Tuple.cs index ebbd156dda..bb72e4f0ec 100644 --- a/src/System.Private.CoreLib/shared/System/Tuple.cs +++ b/src/System.Private.CoreLib/shared/System/Tuple.cs @@ -124,9 +124,7 @@ namespace System { if (other == null) return false; - Tuple<T1> objTuple = other as Tuple<T1>; - - if (objTuple == null) + if (!(other is Tuple<T1> objTuple)) { return false; } @@ -143,9 +141,7 @@ namespace System { if (other == null) return 1; - Tuple<T1> objTuple = other as Tuple<T1>; - - if (objTuple == null) + if (!(other is Tuple<T1> objTuple)) { throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, this.GetType().ToString()), nameof(other)); } @@ -227,9 +223,7 @@ namespace System { if (other == null) return false; - Tuple<T1, T2> objTuple = other as Tuple<T1, T2>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2> objTuple)) { return false; } @@ -246,9 +240,7 @@ namespace System { if (other == null) return 1; - Tuple<T1, T2> objTuple = other as Tuple<T1, T2>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2> objTuple)) { throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, this.GetType().ToString()), nameof(other)); } @@ -345,9 +337,7 @@ namespace System { if (other == null) return false; - Tuple<T1, T2, T3> objTuple = other as Tuple<T1, T2, T3>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3> objTuple)) { return false; } @@ -364,9 +354,7 @@ namespace System { if (other == null) return 1; - Tuple<T1, T2, T3> objTuple = other as Tuple<T1, T2, T3>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3> objTuple)) { throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, this.GetType().ToString()), nameof(other)); } @@ -474,9 +462,7 @@ namespace System { if (other == null) return false; - Tuple<T1, T2, T3, T4> objTuple = other as Tuple<T1, T2, T3, T4>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3, T4> objTuple)) { return false; } @@ -493,9 +479,7 @@ namespace System { if (other == null) return 1; - Tuple<T1, T2, T3, T4> objTuple = other as Tuple<T1, T2, T3, T4>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3, T4> objTuple)) { throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, this.GetType().ToString()), nameof(other)); } @@ -614,9 +598,7 @@ namespace System { if (other == null) return false; - Tuple<T1, T2, T3, T4, T5> objTuple = other as Tuple<T1, T2, T3, T4, T5>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3, T4, T5> objTuple)) { return false; } @@ -633,9 +615,7 @@ namespace System { if (other == null) return 1; - Tuple<T1, T2, T3, T4, T5> objTuple = other as Tuple<T1, T2, T3, T4, T5>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3, T4, T5> objTuple)) { throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, this.GetType().ToString()), nameof(other)); } @@ -765,9 +745,7 @@ namespace System { if (other == null) return false; - Tuple<T1, T2, T3, T4, T5, T6> objTuple = other as Tuple<T1, T2, T3, T4, T5, T6>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3, T4, T5, T6> objTuple)) { return false; } @@ -784,9 +762,7 @@ namespace System { if (other == null) return 1; - Tuple<T1, T2, T3, T4, T5, T6> objTuple = other as Tuple<T1, T2, T3, T4, T5, T6>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3, T4, T5, T6> objTuple)) { throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, this.GetType().ToString()), nameof(other)); } @@ -927,9 +903,7 @@ namespace System { if (other == null) return false; - Tuple<T1, T2, T3, T4, T5, T6, T7> objTuple = other as Tuple<T1, T2, T3, T4, T5, T6, T7>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3, T4, T5, T6, T7> objTuple)) { return false; } @@ -946,9 +920,7 @@ namespace System { if (other == null) return 1; - Tuple<T1, T2, T3, T4, T5, T6, T7> objTuple = other as Tuple<T1, T2, T3, T4, T5, T6, T7>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3, T4, T5, T6, T7> objTuple)) { throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, this.GetType().ToString()), nameof(other)); } @@ -1105,9 +1077,7 @@ namespace System { if (other == null) return false; - Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> objTuple = other as Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> objTuple)) { return false; } @@ -1124,9 +1094,7 @@ namespace System { if (other == null) return 1; - Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> objTuple = other as Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>; - - if (objTuple == null) + if (!(other is Tuple<T1, T2, T3, T4, T5, T6, T7, TRest> objTuple)) { throw new ArgumentException(SR.Format(SR.ArgumentException_TupleIncorrectType, this.GetType().ToString()), nameof(other)); } |