make ConsoleKeyInfo implement IEquatable<ConsoleKeyInfo> (#40660)
authorAdam Sitnik <adam.sitnik@gmail.com>
Tue, 11 Aug 2020 19:08:27 +0000 (21:08 +0200)
committerGitHub <noreply@github.com>
Tue, 11 Aug 2020 19:08:27 +0000 (21:08 +0200)
* make ConsoleKeyInfo implement IEquatable<ConsoleKeyInfo>, fixes #2127

* remove unnecessary cast

src/libraries/System.Console/ref/System.Console.cs
src/libraries/System.Console/src/System/ConsoleKeyInfo.cs
src/libraries/System.Console/tests/ConsoleKeyInfoTests.cs

index 6c17987..b3faa64 100644 (file)
@@ -281,7 +281,7 @@ namespace System
         Pa1 = 253,
         OemClear = 254,
     }
-    public readonly partial struct ConsoleKeyInfo
+    public readonly partial struct ConsoleKeyInfo : System.IEquatable<System.ConsoleKeyInfo>
     {
         private readonly int _dummyPrimitive;
         public ConsoleKeyInfo(char keyChar, System.ConsoleKey key, bool shift, bool alt, bool control) { throw null; }
index b4a8e2f..669416a 100644 (file)
@@ -5,7 +5,7 @@ using System.Diagnostics;
 
 namespace System
 {
-    public readonly struct ConsoleKeyInfo
+    public readonly struct ConsoleKeyInfo : IEquatable<ConsoleKeyInfo>
     {
         private readonly char _keyChar;
         private readonly ConsoleKey _key;
@@ -50,7 +50,7 @@ namespace System
 
         public override bool Equals(object? value)
         {
-            return value is ConsoleKeyInfo && Equals((ConsoleKeyInfo)value);
+            return value is ConsoleKeyInfo info && Equals(info);
         }
 
         public bool Equals(ConsoleKeyInfo obj)
index 2d63cd8..da3720a 100644 (file)
@@ -69,6 +69,9 @@ namespace System.Tests
             Assert.False(default(ConsoleKeyInfo).Equals(new object()));
         }
 
+        [Fact]
+        public void ImplementsIEquatableInterface() => Assert.True(typeof(IEquatable<ConsoleKeyInfo>).IsAssignableFrom(typeof(ConsoleKeyInfo)));
+
         public static readonly object[][] NotEqualConsoleKeyInfos = {
             new object[] { new ConsoleKeyInfo('a', ConsoleKey.A, true, true, true), new ConsoleKeyInfo('b', ConsoleKey.A, true, true, true)  },
             new object[] { new ConsoleKeyInfo('a', ConsoleKey.A, true, true, true), new ConsoleKeyInfo('a', ConsoleKey.B, true, true, true)  },