String.Inequality(String, String) 運算子

定義

判斷兩個指定的字串是否具有不同的值。

C#
public static bool operator !=(string a, string b);
C#
public static bool operator !=(string? a, string? b);

參數

a
String

要比較的第一個字串,或是 null

b
String

要比較的第二個字串,或 null

傳回

如果 true 的值與 a 的值不同,則為 b,否則為 false

範例

下列範例示範不等比較運算子。

C#
// Example for the String Inequality operator.
using System;

class InequalityOp 
{
    public static void Main() 
    {
        Console.WriteLine( 
            "This example of the String Inequality 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 Inequality operator
generates the following output.

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

備註

方法 Inequality 會定義 類別的不等比較運算子 String 運算。 它會啟用程式碼,例如 [範例] 區段中所示的程式碼。

運算子 Inequality 接著會呼叫靜態 Equals(String, String) 方法,它會執行區分大小寫且不區分文化特性的序數 () 比較。

備註

Visual Basic 編譯器不會將不等比較運算子解析為方法的 Inequality 呼叫。 相反地,不等比較運算子會包裝方法的 Operators.CompareString 呼叫。

適用於

產品 版本
.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 1.1, 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.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0