Partager via


Nullable<T>.Equals Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Overrides Function Equals ( _
    other As Object _
) As Boolean
public override bool Equals(
    Object other
)

Parameters

Return Value

Type: System.Boolean
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 nulla null reference (Nothing in Visual Basic). 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 nulla null reference (Nothing in Visual Basic).

-or-

The HasValue property for the current Nullable<T> structure is false, and the other parameter is not nulla null reference (Nothing in Visual Basic).

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

Remarks

If the HasValue property of the current Nullable<T> structure is true, equality is determined by passing the other parameter to the Equals method of the underlying value of the current Nullable<T> structure.

Examples

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(Of T).Equals 
' methods.


Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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.
      outputBlock.Text &= "1) nullInt1 and nullInt2 "
      If nullInt1.Equals(nullInt2) Then
         outputBlock.Text &= "are"
      Else
         outputBlock.Text &= "are not"
      End If
      outputBlock.Text &= " equal." & vbCrLf

      ' 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)
      outputBlock.Text &= "2) nullInt1 and myObj "
      If nullInt1.Equals(myObj) Then
         outputBlock.Text &= "are"
      Else
         outputBlock.Text &= "are not"
      End If
      outputBlock.Text &= " equal." & vbCrLf
   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.
'
// This code example demonstrates the Nullable<T>.Equals 
// methods.

using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      int? nullInt1 = 100;
      int? nullInt2 = 200;
      object myObj;

      // Determine if two nullable of System.Int32 values are equal.
      // The nullable objects have different values.
      outputBlock.Text += "1) nullInt1 and nullInt2 ";
      if (nullInt1.Equals(nullInt2))
         outputBlock.Text += "are";
      else
         outputBlock.Text += "are not";
      outputBlock.Text += " equal." + "\n";

      // 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;
      outputBlock.Text += "2) nullInt1 and myObj ";
      if (nullInt1.Equals(myObj))
         outputBlock.Text += "are";
      else
         outputBlock.Text += "are not";
      outputBlock.Text += " equal." + "\n";
   }
}

/*
This code example produces the following results:

1) nullInt1 and nullInt2 are not equal.
2) nullInt1 and myObj are equal.

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.