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
DateTime.GetTypeCode() Method in C#
The DateTime.GetTypeCode() method in C# is used to return the TypeCode for the DateTime value type. The returned value is the enumerated constant TypeCode.DateTime.
This method is inherited from the IConvertible interface and is primarily used in scenarios where you need to determine the type code of a DateTime object programmatically, such as in reflection or type conversion operations.
Syntax
Following is the syntax −
public TypeCode GetTypeCode();
Return Value
The method returns TypeCode.DateTime, which is an enumerated constant that represents the DateTime type.
Using GetTypeCode() with Current DateTime
The following example demonstrates how to use the GetTypeCode() method with the current system date and time −
using System;
public class Demo {
public static void Main() {
DateTime d = DateTime.Now;
TypeCode res = d.GetTypeCode();
Console.WriteLine("TypeCode of Date {0} = {1}", d, res);
Console.WriteLine("TypeCode value: " + (int)res);
}
}
The output of the above code is −
TypeCode of Date 10/16/2019 7:08:50 AM = DateTime TypeCode value: 16
Using GetTypeCode() with Specific DateTime
The following example shows how to use the GetTypeCode() method with a specifically constructed DateTime object −
using System;
public class Demo {
public static void Main() {
DateTime d = new DateTime(2019, 10, 11, 7, 10, 40);
TypeCode res = d.GetTypeCode();
Console.WriteLine("TypeCode of Date {0} = {1}", d, res);
// Compare with other TypeCodes
Console.WriteLine("Is DateTime TypeCode? " + (res == TypeCode.DateTime));
Console.WriteLine("TypeCode numeric value: " + (int)res);
}
}
The output of the above code is −
TypeCode of Date 10/11/2019 7:10:40 AM = DateTime Is DateTime TypeCode? True TypeCode numeric value: 16
Practical Use Case
The GetTypeCode() method is often used in generic programming scenarios where you need to handle different data types −
using System;
public class TypeHelper {
public static void PrintTypeInfo(object value) {
if (value is IConvertible convertible) {
TypeCode typeCode = convertible.GetTypeCode();
Console.WriteLine("Value: {0}, Type: {1}, TypeCode: {2}",
value, value.GetType().Name, typeCode);
}
}
public static void Main() {
DateTime dateValue = DateTime.Now;
int intValue = 42;
string stringValue = "Hello";
PrintTypeInfo(dateValue);
PrintTypeInfo(intValue);
PrintTypeInfo(stringValue);
}
}
The output of the above code is −
Value: 10/16/2019 7:08:50 AM, Type: DateTime, TypeCode: DateTime Value: 42, Type: Int32, TypeCode: Int32 Value: Hello, Type: String, TypeCode: String
Conclusion
The DateTime.GetTypeCode() method always returns TypeCode.DateTime and is useful in reflection scenarios and generic type handling. While not commonly used in everyday programming, it serves an important role in type identification and conversion operations.
