Nullable<T>.Equals(Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指示目前的 Nullable<T> 物件是否等於指定的物件。
public:
override bool Equals(System::Object ^ other);
public override bool Equals (object other);
public override bool Equals (object? other);
override this.Equals : obj -> bool
Public Overrides Function Equals (other As Object) As Boolean
參數
- other
- Object
物件。
傳回
如果 other
參數等於目前的 Nullable<T> 物件,則為 true
,否則為 false
。
下表描述如何為比較值定義相等比較:
傳回值 | 描述 |
---|---|
true | HasValue 屬性為 false ,而且 other 參數是 null (也就是,兩個 Null 值依定義是相等的),或 HasValue 屬性為 true ,而且 Value 屬性傳回的值等於 other 參數。
|
false | 目前 Nullable<T> 結構的 HasValue 屬性為 true ,而且 other 參數為 null ,或目前 HasValue 結構的 Nullable<T> 屬性為 false ,而且 other 參數不是 null ,或目前 Nullable<T> 結構的 HasValue 屬性是 true ,而且 Value 屬性傳回的值不等於 other 參數。
|
範例
下列程式碼範例會判斷 物件和 Nullable<T> 物件是否等於目前的 Nullable<T> 物件。
// This code example demonstrates the Nullable<T>.Equals
// methods.
using System;
class Sample
{
public static void Main()
{
int? nullInt1 = 100;
int? nullInt2 = 200;
object myObj;
// Determine if two nullable of System.Int32 values are equal.
// The nullable objects have different values.
Console.Write("1) nullInt1 and nullInt2 ");
if (nullInt1.Equals(nullInt2))
Console.Write("are");
else
Console.Write("are not");
Console.WriteLine(" equal.");
// Determine if a nullable of System.Int32 and an object
// are equal. The object contains the boxed value of the
// nullable object.
myObj = (object)nullInt1;
Console.Write("2) nullInt1 and myObj ");
if (nullInt1.Equals(myObj))
Console.Write("are");
else
Console.Write("are not");
Console.WriteLine(" equal.");
}
}
/*
This code example produces the following results:
1) nullInt1 and nullInt2 are not equal.
2) nullInt1 and myObj are equal.
*/
// This code example demonstrates the Nullable<T>.Equals
// methods.
open System
let nullInt1 = Nullable 100
let nullInt2 = Nullable 200
// Determine if two nullable of System.Int32 values are equal.
// The nullable objects have different values.
printf "1) nullInt1 and nullInt2 "
if nullInt1.Equals nullInt1 then
printf "are"
else
printf "are not"
printfn " equal."
// Determine if a nullable of System.Int32 and an object
// are equal. The object contains the boxed value of the
// nullable object.
let myObj = box nullInt1
printf "2) nullInt1 and myObj "
if nullInt1.Equals myObj then
printf "are"
else
printf "are not"
printfn " equal."
// This code example produces the following results:
// 1) nullInt1 and nullInt2 are not equal.
// 2) nullInt1 and myObj are equal.
' This code example demonstrates the Nullable(Of T).Equals
' methods.
Class Sample
Public Shared Sub Main()
Dim nullInt1 As Nullable(Of Integer) = 100
Dim nullInt2 As Nullable(Of Integer) = 200
Dim myObj As Object
' Determine if two nullable of System.Int32 values are equal.
' The nullable objects have different values.
Console.Write("1) nullInt1 and nullInt2 ")
If nullInt1.Equals(nullInt2) Then
Console.Write("are")
Else
Console.Write("are not")
End If
Console.WriteLine(" equal.")
' Determine if a nullable of System.Int32 and an object
' are equal. The object contains the boxed value of the
' nullable object.
myObj = CType(nullInt1, Object)
Console.Write("2) nullInt1 and myObj ")
If nullInt1.Equals(myObj) Then
Console.Write("are")
Else
Console.Write("are not")
End If
Console.WriteLine(" equal.")
End Sub
End Class
'
'This code example produces the following results:
'
'1) nullInt1 and nullInt2 are not equal.
'2) nullInt1 and myObj are equal.
'
備註
HasValue如果目前 Nullable<T> 結構的 屬性是 true
,而且 other
引數不是 null
,則相等是透過將 參數傳遞 other
至 Equals
目前 Nullable<T> 結構之基礎值的 方法來決定。