Skip to main content
NullableEquals Method
 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public override bool Equals(
	object other
)
public:
virtual bool Equals(
	Object^ other
) override
override Equals : 
        other:Object -> bool
Public Overrides Function Equals (
	other As Object
) As Boolean

Parameters

other
Type:

An object.

Return Value

Type:

true if the other parameter is equal to the current Nullable object; otherwise, false.

This table describes how equality is defined for the compared values:

Return Value

Description

true

The property is false, and the other parameter is null. That is, two null values are equal by definition.

-or-

The property is true, and the value returned by the property is equal to the other parameter.

false

The property for the current Nullable structure is true, and the other parameter is null.

-or-

The property for the current Nullable structure is false, and the other parameter is not null.

-or-

The property for the current Nullable structure is true, and the value returned by the property is not equal to the other parameter.

If the property of the current Nullable 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 structure.

The following code example determines whether an object and a Nullable object are equal to the current Nullable 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.

Imports System

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 'Main 
End Class 'Sample

'
'This code example produces the following results:
'
'1) nullInt1 and nullInt2 are not equal.
'2) nullInt1 and myObj are equal.
'
Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top