C# | Convert.GetTypeCode(Object) Method
Last Updated :
02 Sep, 2021
This method is used to return the TypeCode for the specified object.
Syntax:
public static TypeCode GetTypeCode (object value);
Here, the value is an object that implements the IConvertible interface.
Return Value: This method returns the TypeCode for value, or Empty if value is null.
Below programs illustrate the use of Convert.GetTypeCode(Object) Method:
Example 1:
csharp
using System;
class GFG {
public static void Main()
{
string val1;
bool val2;
val1 = "abab" ;
val2 = true ;
TypeCode t1 = Convert.GetTypeCode(val1);
TypeCode t2 = Convert.GetTypeCode(val2);
Console.WriteLine( "TypeCode of val1 is {0}" , t1);
Console.WriteLine( "TypeCode of val2 is {0}" , t2);
}
}
|
Output:
TypeCode of val1 is String
TypeCode of val2 is Boolean
Example 2:
csharp
using System;
class GFG {
public static void Main()
{
char val1;
float val2;
val1 = 'a' ;
val2 = 20f;
get (val1);
get (val2);
}
public static void get ( char val)
{
TypeCode t = Convert.GetTypeCode(val);
Console.WriteLine( "TypeCode of {0}" +
" is {1}" , val, t);
}
public static void get ( float val)
{
TypeCode t = Convert.GetTypeCode(val);
Console.WriteLine( "TypeCode of {0}" +
" is {1}" , val, t);
}
}
|
Output:
TypeCode of a is Char
TypeCode of 20 is Single
Reference:
Share your thoughts in the comments
Please Login to comment...