String.Equality(String, String) 运算符

定义

确定两个指定的字符串是否具有相同的值。

public:
 static bool operator ==(System::String ^ a, System::String ^ b);
public static bool operator == (string a, string b);
public static bool operator == (string? a, string? b);
static member ( = ) : string * string -> bool
Public Shared Operator == (a As String, b As String) As Boolean

参数

a
String

要比较的第一个字符串,或 null

b
String

要比较的第二个字符串,或 null

返回

Boolean

如果 a 的值与 b 的值相同,则为 true;否则为 false

示例

下面的示例演示了相等运算符。

// Example for the String Equality operator.
using namespace System;
void CompareAndDisplay( String^ Comparand )
{
   String^ Lower = "abcd";
   Console::WriteLine( "\"{0}\" == \"{1}\" ?  {2}", Lower, Comparand, Lower == Comparand );
}

int main()
{
   Console::WriteLine( "This example of the String Equality operator\n"
   "generates the following output.\n" );
   CompareAndDisplay( "ijkl" );
   CompareAndDisplay( "ABCD" );
   CompareAndDisplay( "abcd" );
}

/*
This example of the String Equality operator 
generates the following output.

"abcd" == "ijkl" ?  False
"abcd" == "ABCD" ?  False
"abcd" == "abcd" ?  True
*/
// Example for the String Equality operator.
using System;

class EqualityOp 
{
    public static void Main() 
    {
        Console.WriteLine( 
            "This example of the String Equality operator\n" +
            "generates the following output.\n" );

        CompareAndDisplay( "ijkl" );
        CompareAndDisplay( "ABCD" );
        CompareAndDisplay( "abcd" );
    }

    static void CompareAndDisplay( string Comparand )
    {
        String  Lower = "abcd";

        Console.WriteLine( 
            "\"{0}\" == \"{1}\" ?  {2}",
            Lower, Comparand, Lower == Comparand );
    }
}

/*
This example of the String Equality operator 
generates the following output.

"abcd" == "ijkl" ?  False
"abcd" == "ABCD" ?  False
"abcd" == "abcd" ?  True
*/

注解

Equality方法定义类的相等运算符的运算 String 。 它将启用示例部分中所示的代码。 运算符反过来调用静态 Equals(String, String) 方法,该方法执行 (区分大小写和不区分区域性的) 比较。

备注

Visual Basic 编译器不会将相等运算符解析为对方法的调用 Equality 。 相反,相等运算符包装对方法的调用 Operators.CompareString

适用于