C# | Convert.GetTypeCode(Object) Method
Last Updated :
02 Sep, 2021
Improve
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:
// C# program to demonstrate
// Convert.GetTypeCode() Method
using System;
class GFG {
// Main Method
public static void Main()
{
// Declaring val1 and val2
string val1;
bool val2;
// initializing the
// val1 and val2
val1 = "abab";
val2 = true;
// getting TypeCode
// using Convert.GetTypeCode() method
TypeCode t1 = Convert.GetTypeCode(val1);
TypeCode t2 = Convert.GetTypeCode(val2);
// Display TypeCode
Console.WriteLine("TypeCode of val1 is {0}", t1);
Console.WriteLine("TypeCode of val2 is {0}", t2);
}
}
29
1
// C# program to demonstrate
2
// Convert.GetTypeCode() Method
3
using System;
4
5
class GFG {
6
7
// Main Method
8
public static void Main()
9
{
10
11
// Declaring val1 and val2
12
string val1;
13
bool val2;
14
15
// initializing the
16
// val1 and val2
17
val1 = "abab";
18
val2 = true;
19
20
// getting TypeCode
21
// using Convert.GetTypeCode() method
22
TypeCode t1 = Convert.GetTypeCode(val1);
23
TypeCode t2 = Convert.GetTypeCode(val2);
24
25
// Display TypeCode
26
Console.WriteLine("TypeCode of val1 is {0}", t1);
27
Console.WriteLine("TypeCode of val2 is {0}", t2);
28
}
29
}
Output:
TypeCode of val1 is String TypeCode of val2 is Boolean
Example 2:
// C# program to demonstrate
// Convert.GetTypeCode() Method
using System;
class GFG {
// Main Method
public static void Main()
{
// Declaring val1 and val2
char val1;
float val2;
// initializing the
// val1 and val2
val1 = 'a';
val2 = 20f;
// calling get() method
get(val1);
get(val2);
}
// Defining the get() method
public static void get(char val)
{
// getting TypeCode
// using Convert.GetTypeCode() method
TypeCode t = Convert.GetTypeCode(val);
// Display the TypeCode
Console.WriteLine("TypeCode of {0}"+
" is {1}", val, t);
}
public static void get(float val)
{
// getting TypeCode
// using GetTypeCode() method
TypeCode t = Convert.GetTypeCode(val);
// Display the TypeCode
Console.WriteLine("TypeCode of {0}"+
" is {1}", val, t);
}
}
48
1
// C# program to demonstrate
2
// Convert.GetTypeCode() Method
3
using System;
4
5
class GFG {
6
7
// Main Method
8
public static void Main()
9
{
10
11
// Declaring val1 and val2
12
char val1;
13
float val2;
14
15
// initializing the
16
// val1 and val2
17
val1 = 'a';
18
val2 = 20f;
19
20
// calling get() method
21
get(val1);
22
get(val2);
23
}
24
25
// Defining the get() method
26
public static void get(char val)
27
{
28
29
// getting TypeCode
30
// using Convert.GetTypeCode() method
31
TypeCode t = Convert.GetTypeCode(val);
32
33
// Display the TypeCode
34
Console.WriteLine("TypeCode of {0}"+
35
" is {1}", val, t);
36
}
37
public static void get(float val)
38
{
39
40
// getting TypeCode
41
// using GetTypeCode() method
42
TypeCode t = Convert.GetTypeCode(val);
43
44
// Display the TypeCode
45
Console.WriteLine("TypeCode of {0}"+
46
" is {1}", val, t);
47
}
48
}
Output:
TypeCode of a is Char TypeCode of 20 is Single
Reference: