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.GetTypeCode() Method in C# with Examples
The UInt32.GetTypeCode() method in C# returns the TypeCode enumeration value for the UInt32 data type. This method is inherited from the IConvertible interface and is used to identify the underlying type of a value at runtime.
The TypeCode enumeration provides a way to categorize the fundamental data types in .NET, making it useful for type checking and conversion operations.
Syntax
Following is the syntax for the UInt32.GetTypeCode() method −
public TypeCode GetTypeCode();
Return Value
This method returns TypeCode.UInt32, which represents the enumerated constant for the 32-bit unsigned integer type.
Using GetTypeCode() with UInt32 Values
Example
The following example demonstrates how to get the type code for different UInt32 values −
using System;
public class Demo {
public static void Main() {
uint val1 = 55;
uint val2 = 100;
TypeCode type1 = val1.GetTypeCode();
TypeCode type2 = val2.GetTypeCode();
Console.WriteLine("Typecode for val1 = " + type1);
Console.WriteLine("Typecode for val2 = " + type2);
}
}
The output of the above code is −
Typecode for val1 = UInt32 Typecode for val2 = UInt32
Using GetTypeCode() with UInt32 Boundary Values
Example
This example shows how the method works with boundary values like minimum and zero −
using System;
public class Demo {
public static void Main() {
uint val1 = UInt32.MinValue;
uint val2 = UInt32.MaxValue;
uint val3 = 0;
TypeCode type1 = val1.GetTypeCode();
TypeCode type2 = val2.GetTypeCode();
TypeCode type3 = val3.GetTypeCode();
Console.WriteLine("Typecode for MinValue = " + type1);
Console.WriteLine("Typecode for MaxValue = " + type2);
Console.WriteLine("Typecode for zero = " + type3);
Console.WriteLine("MinValue: " + val1);
Console.WriteLine("MaxValue: " + val2);
}
}
The output of the above code is −
Typecode for MinValue = UInt32 Typecode for MaxValue = UInt32 Typecode for zero = UInt32 MinValue: 0 MaxValue: 4294967295
Practical Use Case
Example
This example demonstrates how GetTypeCode() can be used for type checking in a generic method −
using System;
public class Demo {
public static void CheckType(object value) {
if (value is IConvertible convertible) {
TypeCode typeCode = convertible.GetTypeCode();
Console.WriteLine($"Value: {value}, TypeCode: {typeCode}");
if (typeCode == TypeCode.UInt32) {
Console.WriteLine("This is a 32-bit unsigned integer");
}
}
}
public static void Main() {
uint uintVal = 12345;
int intVal = -12345;
string strVal = "Hello";
CheckType(uintVal);
CheckType(intVal);
CheckType(strVal);
}
}
The output of the above code is −
Value: 12345, TypeCode: UInt32 This is a 32-bit unsigned integer Value: -12345, TypeCode: Int32 Value: Hello, TypeCode: String
Conclusion
The UInt32.GetTypeCode() method consistently returns TypeCode.UInt32 for any 32-bit unsigned integer value. This method is particularly useful for runtime type identification and implementing type-safe operations in generic programming scenarios.
