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 Struct in C#
The UInt16 struct represents a 16-bit unsigned integer. The UInt16 value type represents unsigned integers with values ranging from 0 to 65,535.
The UInt16 struct provides several useful methods for comparison, equality checking, and string conversion operations. Let us explore the key methods with examples.
UInt16.CompareTo() Method
The UInt16.CompareTo() method compares the current instance to a specified object or UInt16 and returns an indication of their relative values.
Syntax
Following are the syntax overloads −
public int CompareTo(object val); public int CompareTo(ushort val);
Parameters
-
val − An object or unsigned integer to compare with the current instance.
Return Value
-
0 − If the current instance is equal to the value.
-
Less than 0 − If the current instance is less than the value.
-
Greater than 0 − If the current instance is greater than the value.
Example
The following example demonstrates comparing two UInt16 values −
using System;
public class Demo {
public static void Main() {
ushort val1 = 25;
ushort val2 = 55;
int res = val1.CompareTo(val2);
Console.WriteLine("Return value (comparison) = " + res);
if (res > 0)
Console.WriteLine("val1 > val2");
else if (res
The output of the above code is −
Return value (comparison) = -1
val1
Example Using Object Parameter
The following example shows comparison with an object parameter −
using System;
public class Demo {
public static void Main() {
ushort val1 = 25;
object val2 = (ushort)2;
int res = val1.CompareTo(val2);
Console.WriteLine("Return value (comparison) = " + res);
if (res > 0)
Console.WriteLine("val1 > val2");
else if (res
The output of the above code is −
Return value (comparison) = 1
val1 > val2
UInt16.Equals() Method
The UInt16.Equals() method returns a boolean value indicating whether the current instance is equal to a specified object or UInt16.
Syntax
Following are the syntax overloads −
public override bool Equals(object obj);
public bool Equals(ushort obj);
Parameters
-
obj − An object or 16-bit unsigned integer to compare with the current instance.
Return Value
-
true − If the values are equal.
-
false − If the values are not equal.
Example
The following example demonstrates equality comparison with unequal 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
Example with Equal Values
The following example demonstrates equality comparison with equal 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
Common Use Cases
The UInt16 struct is commonly used when working with:
-
Port numbers − Network port values range from 0 to 65535.
-
Memory addressing − 16-bit addressing systems.
-
Color values − RGB color components in graphics programming.
-
Binary data processing − When working with 16-bit data structures.
Conclusion
The UInt16 struct provides essential methods like CompareTo() and Equals() for comparing unsigned 16-bit integers. These methods support both direct value comparison and object-based comparison, making them versatile for various programming scenarios involving numerical data within the 0 to 65,535 range.
