Get a value indicating whether this instance is equal to a specified object or UInt64 in C#

The UInt64.Equals() method in C# determines whether the current UInt64 instance is equal to a specified object or another UInt64 value. This method returns true if the values are equal, otherwise false.

The Equals() method is inherited from the Object class and overridden in the UInt64 structure to provide value-based comparison rather than reference comparison.

Syntax

Following is the syntax for the UInt64.Equals() method −

public bool Equals(object obj)
public bool Equals(ulong value)

Parameters

  • obj − The object to compare with the current instance.

  • value − A UInt64 value to compare with the current instance.

Return Value

Returns true if the specified object or value is equal to the current UInt64 instance; otherwise, false.

Using Equals() with UInt64 Values

Example 1 - Comparing Different UInt64 Values

using System;

public class Demo {
   public static void Main() {
      ulong val1 = 44565777;
      ulong val2 = 77878787;
      bool res = val1.Equals(val2);
      Console.WriteLine("Return value (comparison) = " + res);
      if (res)
         Console.WriteLine("val1 = val2");
      else
         Console.WriteLine("val1 != val2");
   }
}

The output of the above code is −

Return value (comparison) = False
val1 != val2

Example 2 - Comparing Equal UInt64 Values

using System;

public class Demo {
   public static void Main() {
      ulong val1 = 78796878;
      ulong val2 = 78796878;
      bool res = val1.Equals(val2);
      Console.WriteLine("Return value (comparison) = " + res);
      if (res)
         Console.WriteLine("val1 = val2");
      else
         Console.WriteLine("val1 != val2");
   }
}

The output of the above code is −

Return value (comparison) = True
val1 = val2

Using Equals() with Object Parameter

Example

using System;

public class Demo {
   public static void Main() {
      ulong val1 = 12345678;
      object obj1 = 12345678UL;
      object obj2 = "12345678";
      object obj3 = 87654321UL;
      
      Console.WriteLine("val1.Equals(obj1): " + val1.Equals(obj1));
      Console.WriteLine("val1.Equals(obj2): " + val1.Equals(obj2));
      Console.WriteLine("val1.Equals(obj3): " + val1.Equals(obj3));
   }
}

The output of the above code is −

val1.Equals(obj1): True
val1.Equals(obj2): False
val1.Equals(obj3): False

Comparison with Other Methods

Method Description Usage
Equals() Instance method for value comparison val1.Equals(val2)
== operator Equality operator for UInt64 val1 == val2
CompareTo() Returns -1, 0, or 1 for ordering val1.CompareTo(val2)

Conclusion

The UInt64.Equals() method provides a reliable way to compare UInt64 values for equality. It returns true when values are identical and false otherwise, making it essential for conditional logic and value validation in C# applications.

Updated on: 2026-03-17T07:04:36+05:30

147 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements