UInt64.Equals Method in C# with Examples

The UInt64.Equals() method in C# returns a value indicating whether this instance is equal to a specified object or UInt64. This method provides a way to compare 64-bit unsigned integer values for equality, offering both generic object comparison and type-specific comparison overloads.

Syntax

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

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

Parameters

The UInt64.Equals() method accepts the following parameters −

  • obj − An object to compare to this instance (first overload)

  • value − A 64-bit unsigned integer to compare to this instance (second overload)

Return Value

The method returns a bool value −

  • true if the specified value is equal to this instance

  • false if the specified value is not equal to this instance

Using UInt64.Equals() with Different Values

The following example demonstrates comparing two 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

Using UInt64.Equals() with Equal Values

The following example demonstrates comparing two 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 UInt64.Equals() with Object Parameter

The following example demonstrates using the object overload of the Equals() method −

using System;

public class Demo {
   public static void Main() {
      ulong val1 = 123456789;
      object obj1 = (ulong)123456789;
      object obj2 = "123456789";
      object obj3 = 123456789; // int, not ulong
      
      Console.WriteLine("Comparing with ulong object: " + val1.Equals(obj1));
      Console.WriteLine("Comparing with string object: " + val1.Equals(obj2));
      Console.WriteLine("Comparing with int object: " + val1.Equals(obj3));
   }
}

The output of the above code is −

Comparing with ulong object: True
Comparing with string object: False
Comparing with int object: False

Conclusion

The UInt64.Equals() method provides a reliable way to compare 64-bit unsigned integer values for equality. It offers both type-specific and object-based comparison, returning true only when the values are exactly equal and of the correct type.

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

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements