Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
UInt16.Equals Method in C# with Examples
The UInt16.Equals() method in C# returns a value indicating whether this instance is equal to a specified object or UInt16. This method provides two overloads: one for comparing with any object, and another specifically for comparing with another ushort value.
Syntax
Following are the two overloaded syntax forms −
public override bool Equals (object ob); public bool Equals (ushort ob);
Parameters
ob (first overload) − An object to compare to this instance.
ob (second overload) − The 16-bit unsigned integer to compare to this instance.
Return Value
Returns true if the specified value is equal to this instance; otherwise, false.
Using UInt16.Equals() with Different Values
The following example demonstrates comparing two different ushort values −
using System;
public class Demo {
public static void Main() {
ushort val1 = 52;
ushort 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 UInt16.Equals() with Same Values
The following example demonstrates comparing two identical ushort values −
using System;
public class Demo {
public static void Main() {
ushort val1 = 100;
ushort 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 UInt16.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() {
ushort val1 = 250;
object obj = (ushort)250;
string str = "250";
Console.WriteLine("Comparing with ushort object: " + val1.Equals(obj));
Console.WriteLine("Comparing with string object: " + val1.Equals(str));
Console.WriteLine("Comparing with null: " + val1.Equals(null));
}
}
The output of the above code is −
Comparing with ushort object: True Comparing with string object: False Comparing with null: False
Conclusion
The UInt16.Equals() method provides a reliable way to compare ushort values for equality. The method returns true when values are identical and false otherwise, with the object overload performing type checking before comparison.
