Nullable<T>.Equals(Object) 方法

定义

指示当前 Nullable<T> 对象是否与指定的对象相等。Indicates whether the current Nullable<T> object is equal to a specified object.

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

一个对象。An object.

返回

Boolean

如果 other 等于当前的 Nullable<T> 对象,则为 true;否则为 falsetrue if the other parameter is equal to the current Nullable<T> object; otherwise, false.

此表描述如何定义所比较值的相等性:This table describes how equality is defined for the compared values:

返回值Return Value 描述Description
true HasValue 属性为 false,并且 other 参数为 null(也就是说,根据定义,两个空值相等),或者,HasValue 属性为 true,并且 Value 属性返回的值等于 other 参数。The HasValue property is false, and the other parameter is null (that is, two null values are equal by definition), OR the HasValue property is true, and the value returned by the Value property is equal to the other parameter.
false 当前 Nullable<T> 结构的 HasValue 属性为 true 并且 other 参数为 null。或者,当前 Nullable<T> 结构的 HasValue 属性为 false,并且 other 参数不为 null。或者,当前 Nullable<T> 结构的 HasValue 属性为 true,并且 Value 属性返回的值不等于 other 参数。The HasValue property for the current Nullable<T> structure is true, and the other parameter is null, OR the HasValue property for the current Nullable<T> structure is false, and the other parameter is not null, OR the HasValue property for the current Nullable<T> structure is true, and the value returned by the Value property is not equal to the other parameter.

示例

下面的代码示例确定对象和对象是否与 Nullable<T> 当前 Nullable<T> 对象相等。The following code example determines whether an object and a Nullable<T> object are equal to the current Nullable<T> object.

// 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(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>If the HasValue property of the current Nullable<T> structure is true and the other argument is not null, equality is determined by passing the other parameter to the Equals method of the underlying value of the current Nullable<T> structure.

适用于