String.Equality(String, String) 運算子

定義

判斷兩個指定的字串是否具有相同的值。Determines whether two specified strings have the same value.

public:
 static bool operator ==(System::String ^ a, System::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

要比較的第一個字串,或是 nullThe first string to compare, or null.

b
String

要比較的第二個字串,或 nullThe second string to compare, or null.

傳回

如果 true 的值與 a 的值相同,則為 b,否則為 falsetrue if the value of a is the same as the value of b; otherwise, false.

範例

下列範例示範等號比較運算子。The following example demonstrates the equality operator.

// 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 類別的等號比較運算子的運算。The Equality method defines the operation of the equality operator for the String class. 它會啟用如範例一節所示的程式碼。It enables code such as that shown in the Example section. 接著,運算子會呼叫靜態 Equals(String, String) 方法,其會執行序數(區分大小寫且不區分文化特性)的比較。The operator, in turn, calls the static Equals(String, String) method, which performs an ordinal (case-sensitive and culture-insensitive) comparison.

注意

Visual Basic 編譯器不會將等號比較運算子解析為 Equality 方法的呼叫。The Visual Basic compiler does not resolve the equality operator as a call to the Equality method. 相反地,等號比較運算子會包裝對 Operators.CompareString 方法的呼叫。Instead, the equality operator wraps a call to the Operators.CompareString method.

適用於