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
Get the TypeCode for value type Char in C#
The TypeCode for a char value type in C# can be obtained using the GetTypeCode() method. This method returns TypeCode.Char, which is an enumeration value that identifies the char data type.
The GetTypeCode() method is inherited from the IConvertible interface and provides a way to determine the underlying type of a value at runtime.
Syntax
Following is the syntax to get the TypeCode for a char value −
TypeCode typeCode = charVariable.GetTypeCode();
Return Value
The method returns TypeCode.Char for char data types, which is part of the TypeCode enumeration.
Using GetTypeCode() with Char
Example
using System;
public class Demo {
public static void Main() {
char val = '5';
bool res;
Console.WriteLine("Hashcode for val = " + val.GetHashCode());
res = val.Equals('m');
Console.WriteLine("Return Value = " + res);
Console.WriteLine("Numeric Value = " + Char.GetNumericValue(val));
TypeCode type = val.GetTypeCode();
Console.WriteLine("Type = " + type);
}
}
The output of the above code is −
Hashcode for val = 3473461 Return Value = False Numeric Value = 5 Type = Char
Simple TypeCode Example
Example
using System;
public class Demo {
public static void Main() {
char val = 'B';
TypeCode type = val.GetTypeCode();
Console.WriteLine("Type = " + type);
}
}
The output of the above code is −
Type = Char
Comparing TypeCodes of Different Data Types
Example
using System;
public class Demo {
public static void Main() {
char charVal = 'A';
string stringVal = "A";
int intVal = 65;
Console.WriteLine("Char TypeCode: " + charVal.GetTypeCode());
Console.WriteLine("String TypeCode: " + stringVal.GetTypeCode());
Console.WriteLine("Int TypeCode: " + intVal.GetTypeCode());
Console.WriteLine("Are char and string TypeCodes equal? " + (charVal.GetTypeCode() == stringVal.GetTypeCode()));
}
}
The output of the above code is −
Char TypeCode: Char String TypeCode: String Int TypeCode: Int32 Are char and string TypeCodes equal? False
Common TypeCode Values
| Data Type | TypeCode |
|---|---|
| char | TypeCode.Char |
| string | TypeCode.String |
| int | TypeCode.Int32 |
| double | TypeCode.Double |
| bool | TypeCode.Boolean |
Conclusion
The GetTypeCode() method for char variables always returns TypeCode.Char. This method is useful for runtime type checking and can help distinguish between different data types when working with generic code or type conversions.
