UInt32.Equals() Method in C# with Examples

The UInt32.Equals() method in C# returns a value indicating whether this instance is equal to a specified object or UInt32. This method provides two overloads for comparing 32-bit unsigned integers with different parameter types.

Syntax

Following are the two overloads of the UInt32.Equals() method −

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

Parameters

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

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

Return Value

Both methods return 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 Equals() with UInt32 Values

The following example demonstrates comparing two uint values using the Equals() method −

using System;

public class Demo {
   public static void Main(){
      uint val1 = 52;
      uint val2 = 10;
      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 Equals() with Equal Values

The following example shows the behavior when comparing equal uint values −

using System;

public class Demo {
   public static void Main(){
      uint val1 = 100;
      uint val2 = 100;
      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

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

using System;

public class Demo {
   public static void Main(){
      uint val1 = 250;
      object obj1 = (uint)250;
      object obj2 = (int)250;
      
      Console.WriteLine("Comparing with uint object: " + val1.Equals(obj1));
      Console.WriteLine("Comparing with int object: " + val1.Equals(obj2));
      Console.WriteLine("Comparing with null: " + val1.Equals(null));
   }
}

The output of the above code is −

Comparing with uint object: True
Comparing with int object: False
Comparing with null: False

Comparison of Both Overloads

Equals(object obj) Equals(uint value)
Can compare with any object type Only compares with uint values
Returns false for null or different types Type-safe comparison
Involves boxing/unboxing overhead Direct comparison, more efficient

Conclusion

The UInt32.Equals() method provides a reliable way to compare 32-bit unsigned integers for equality. Use the Equals(uint) overload for type-safe comparisons and Equals(object) when working with generic object references.

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

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements