Прочетете на английски Редактиране

Споделяне чрез


Nullable<T>.Equals(Object) Method

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Indicates whether the current Nullable<T> object is equal to a specified object.

C#
public override bool Equals(object other);
C#
public override bool Equals(object? other);

Parameters

other
Object

An object.

Returns

true 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 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 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.

Examples

The following code example determines whether an object and a Nullable<T> object are equal to the current Nullable<T> object.

C#
// 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.

*/

Remarks

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.

Applies to

Продукт Версии
.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 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