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
UInt32 Struct in C#
The UInt32 struct represents a 32-bit unsigned integer in C#. The UInt32 value type represents unsigned integers with values ranging from 0 to 4,294,967,295 (2³² - 1).
This struct provides several useful methods for comparison, equality checking, and value manipulation. Let us explore the key methods with practical examples.
Syntax
Following is the syntax for declaring a UInt32 variable −
uint variableName = value; UInt32 variableName = value;
The uint keyword is an alias for UInt32 and both can be used interchangeably.
UInt32.CompareTo() Method
The UInt32.CompareTo() method compares the current instance to a specified object or UInt32 value and returns an indication of their relative values.
Syntax
Following are the overloads for this method −
public int CompareTo(object val); public int CompareTo(uint val);
Return Value
0 if the current instance equals the specified value
Less than 0 if the current instance is less than the specified value
Greater than 0 if the current instance is greater than the specified value
Example
The following example demonstrates comparing two uint values −
using System;
public class Demo {
public static void Main() {
uint val1 = 25;
uint 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 with Object Parameter
The following example shows comparison with an object parameter −
using System;
public class Demo {
public static void Main() {
uint val1 = 25;
object val2 = (uint)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
UInt32.Equals() Method
The UInt32.Equals() method returns a value indicating whether this instance is equal to a specified object or UInt32 value.
Syntax
Following are the overloads for this method −
public override bool Equals(object obj);
public bool Equals(uint obj);
Example
The following example demonstrates equality comparison between two different values −
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
Example with Equal Values
The following example shows equality comparison with identical 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
Common Use Cases
UInt32 is commonly used when you need to store large positive integers, such as file sizes, memory addresses, array indices, or counts where negative values are not meaningful. It provides twice the positive range of a signed 32-bit integer.
Conclusion
The UInt32 struct in C# provides a 32-bit unsigned integer type with a range from 0 to 4,294,967,295. Its CompareTo() and Equals() methods enable efficient comparison and equality operations, making it useful for scenarios requiring large positive integer values.
