From da459a135f6886dfcdba321bf1cd019988dfef51 Mon Sep 17 00:00:00 2001 From: James Ko Date: Fri, 7 Aug 2015 13:05:11 -0400 Subject: [PATCH] Remove unneeded casts to IDisposable and ConcurrentDictionary Commit migrated from https://github.com/dotnet/corefx/commit/fa6e6a1e6300cdbffa31998a8c70d3bb9b7d06c3 --- .../Collections/Concurrent/ConcurrentDictionary.cs | 2 +- .../System.Console/src/System/IO/SyncTextReader.cs | 7 +++---- .../System.Console/src/System/IO/SyncTextWriter.cs | 5 ++--- .../System.Linq/src/System/Linq/Enumerable.cs | 14 ++++++++++---- .../src/System/Xml/Core/XmlWrappingWriter.cs | 2 +- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs index 623d42cf8c9..26b0ca8b0d3 100644 --- a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs +++ b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs @@ -1359,7 +1359,7 @@ namespace System.Collections.Concurrent { if (key == null) throw new ArgumentNullException("key"); - return (key is TKey) && ((ConcurrentDictionary)this).ContainsKey((TKey)key); + return (key is TKey) && this.ContainsKey((TKey)key); } /// Provides an for the diff --git a/src/libraries/System.Console/src/System/IO/SyncTextReader.cs b/src/libraries/System.Console/src/System/IO/SyncTextReader.cs index f6d8a7e9d72..d9735d2d84b 100644 --- a/src/libraries/System.Console/src/System/IO/SyncTextReader.cs +++ b/src/libraries/System.Console/src/System/IO/SyncTextReader.cs @@ -16,7 +16,7 @@ namespace System.IO public static SyncTextReader GetSynchronizedTextReader(TextReader reader) { Debug.Assert(reader != null); - return reader as SyncTextReader ?? + return reader as SyncTextReader ?? new SyncTextReader(reader); } @@ -31,8 +31,7 @@ namespace System.IO { lock (_methodLock) { - // Explicitly pick up a potentially methodimpl'ed Dispose - ((IDisposable)_in).Dispose(); + _in.Dispose(); } } } @@ -87,7 +86,7 @@ namespace System.IO // // On SyncTextReader all APIs should run synchronously, even the async ones. - // No explicit locking is needed, as they all just delegate + // No explicit locking is needed, as they all just delegate // public override Task ReadLineAsync() diff --git a/src/libraries/System.Console/src/System/IO/SyncTextWriter.cs b/src/libraries/System.Console/src/System/IO/SyncTextWriter.cs index c2c89b3615b..c4b9d099a19 100644 --- a/src/libraries/System.Console/src/System/IO/SyncTextWriter.cs +++ b/src/libraries/System.Console/src/System/IO/SyncTextWriter.cs @@ -15,7 +15,7 @@ namespace System.IO internal static SyncTextWriter GetSynchronizedTextWriter(TextWriter writer) { Debug.Assert(writer != null); - return writer as SyncTextWriter ?? + return writer as SyncTextWriter ?? new SyncTextWriter(writer); } @@ -47,8 +47,7 @@ namespace System.IO { lock (_methodLock) { - // Explicitly pick up a potentially methodimpl'ed Dispose - ((IDisposable)_out).Dispose(); + _out.Dispose(); } } } diff --git a/src/libraries/System.Linq/src/System/Linq/Enumerable.cs b/src/libraries/System.Linq/src/System/Linq/Enumerable.cs index 9c6d63ea648..74b3adf6e05 100644 --- a/src/libraries/System.Linq/src/System/Linq/Enumerable.cs +++ b/src/libraries/System.Linq/src/System/Linq/Enumerable.cs @@ -184,8 +184,11 @@ namespace System.Linq public override void Dispose() { - if (_enumerator is IDisposable) ((IDisposable)_enumerator).Dispose(); - _enumerator = null; + if (_enumerator != null) + { + _enumerator.Dispose(); + _enumerator = null; + } base.Dispose(); } @@ -347,8 +350,11 @@ namespace System.Linq public override void Dispose() { - if (_enumerator is IDisposable) ((IDisposable)_enumerator).Dispose(); - _enumerator = null; + if (_enumerator != null) + { + _enumerator.Dispose(); + _enumerator = null; + } base.Dispose(); } diff --git a/src/libraries/System.Xml.ReaderWriter/src/System/Xml/Core/XmlWrappingWriter.cs b/src/libraries/System.Xml.ReaderWriter/src/System/Xml/Core/XmlWrappingWriter.cs index 0eccae59cad..dc72221e2da 100644 --- a/src/libraries/System.Xml.ReaderWriter/src/System/Xml/Core/XmlWrappingWriter.cs +++ b/src/libraries/System.Xml.ReaderWriter/src/System/Xml/Core/XmlWrappingWriter.cs @@ -197,7 +197,7 @@ namespace System.Xml { if (disposing) { - ((IDisposable)writer).Dispose(); + writer.Dispose(); } } }