Прочитать на английском

Поделиться через


ConsoleKeyInfo.Equals Метод

Определение

Возвращает значение, указывающее, равен ли объект или заданный объект ConsoleKeyInfo текущему объекту ConsoleKeyInfo.

Перегрузки

Equals(ConsoleKeyInfo)

Возвращает значение, позволяющее определить, равен ли указанный объект ConsoleKeyInfo текущему объекту ConsoleKeyInfo.

Equals(Object)

Возвращает значение, позволяющее определить, равен ли указанный объект текущему объекту ConsoleKeyInfo.

Equals(ConsoleKeyInfo)

Исходный код:
ConsoleKeyInfo.cs
Исходный код:
ConsoleKeyInfo.cs
Исходный код:
ConsoleKeyInfo.cs

Возвращает значение, позволяющее определить, равен ли указанный объект ConsoleKeyInfo текущему объекту ConsoleKeyInfo.

public bool Equals(ConsoleKeyInfo obj);

Параметры

obj
ConsoleKeyInfo

Объект, который требуется сравнить с текущим объектом ConsoleKeyInfo.

Возвращаемое значение

true, если obj равен текущему объекту ConsoleKeyInfo. В противном случае — false.

Реализации

Комментарии

Два ConsoleKeyInfo объекта равны, если их соответствующие KeyCharсвойства , Keyи Modifiers равны.

Метод Equals работает немного лучше, чем метод , ConsoleKeyInfo.Equals(Object) так как ему не нужно преобразовывать obj в объект .

Применяется к

.NET 10 и другие версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1

Equals(Object)

Исходный код:
ConsoleKeyInfo.cs
Исходный код:
ConsoleKeyInfo.cs
Исходный код:
ConsoleKeyInfo.cs

Возвращает значение, позволяющее определить, равен ли указанный объект текущему объекту ConsoleKeyInfo.

public override bool Equals(object? value);
public override bool Equals(object value);

Параметры

value
Object

Объект, который требуется сравнить с текущим объектом ConsoleKeyInfo.

Возвращаемое значение

Значение true, если value является объектом ConsoleKeyInfo и равен текущему объекту ConsoleKeyInfo. В противном случае — значение false.

Примеры

В следующем примере демонстрируется Equals метод.

// This example demonstrates the ConsoleKeyInfo.Equals() method.

using System;
using System.Text;

class Sample
{
    public static void Main()
    {
    string k1 = "\nEnter a key ......... ";
    string k2 = "\nEnter another key ... ";
    string key1 = "";
    string key2 = "";
    string areKeysEqual = "The {0} and {1} keys are {2}equal.";
    string equalValue = "";
    string prompt = "Press the escape key (ESC) to quit, " +
                    "or any other key to continue.";
    ConsoleKeyInfo cki1;
    ConsoleKeyInfo cki2;

//
// The Console.TreatControlCAsInput property prevents this example from
// ending if you press CTL+C, however all other operating system keys and
// shortcuts, such as ALT+TAB or the Windows Logo key, are still in effect.
//
    Console.TreatControlCAsInput = true;

// Request that the user enter two key presses. A key press and any
// combination shift, CTRL, and ALT modifier keys is permitted.
    do
    {
        Console.Write(k1);
        cki1 = Console.ReadKey(false);
        Console.Write(k2);
        cki2 = Console.ReadKey(false);
        Console.WriteLine();
//
        key1 = KeyCombination(cki1);
        key2 = KeyCombination(cki2);
        if (cki1.Equals(cki2))
            equalValue = "";
        else
            equalValue = "not ";
        Console.WriteLine(areKeysEqual, key1, key2, equalValue);
//
        Console.WriteLine(prompt);
        cki1 = Console.ReadKey(true);
    } while (cki1.Key != ConsoleKey.Escape);
// Note: This example requires the Escape (Esc) key.
    }

// The KeyCombination() method creates a string that specifies what
// key and what combination of shift, CTRL, and ALT modifier keys
// were pressed simultaneously.

    protected static string KeyCombination(ConsoleKeyInfo sourceCki)
    {
    StringBuilder sb = new StringBuilder();
    sb.Length = 0;
    string keyCombo;
    if (sourceCki.Modifiers != 0)
        {
        if ((sourceCki.Modifiers & ConsoleModifiers.Alt) != 0)
            sb.Append("ALT+");
        if ((sourceCki.Modifiers & ConsoleModifiers.Shift) != 0)
            sb.Append("SHIFT+");
        if ((sourceCki.Modifiers & ConsoleModifiers.Control) != 0)
            sb.Append("CTL+");
        }
    sb.Append(sourceCki.Key.ToString());
    keyCombo = sb.ToString();
    return keyCombo;
    }
}

/*
This example produces results similar to the following output:

Enter a key ......... a
Enter another key ... a
The A and A keys are equal.
Press the escape key (ESC) to quit, or any other key to continue.

Enter a key ......... a
Enter another key ... A
The A and SHIFT+A keys are not equal.
Press the escape key (ESC) to quit, or any other key to continue.

Enter a key ......... S
Enter another key ...
The ALT+SHIFT+S and ALT+CTL+F keys are not equal.
Press the escape key (ESC) to quit, or any other key to continue.

Enter a key .........
Enter another key ...
The UpArrow and UpArrow keys are equal.
Press the escape key (ESC) to quit, or any other key to continue.

*/

Комментарии

Два ConsoleKeyInfo объекта равны, если их соответствующие KeyCharсвойства , Keyи Modifiers равны.

Применяется к

.NET 10 и другие версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1