前言:本博文列举了三种判断传入值的数据类型的方法。
一、GetType方法
a.GetType():获取当前变量的类型对象
string str = "Hello World";
Console.WriteLine(str.GetType());
注意:其他数据类型转换可仿照以上案例。
二、typeof方法
typeof(Int):获取的是Int类型的类型对象
int num = 10;
Console.WriteLine(num.GetType() == typeof(int));
注意:其他数据类型转换可仿照以上案例。
三、is方法
a is Enum:获取一个boolean值,表示a是否是Enum类型或者可以隐式向上转型成为Enum类型的类型
enum Sex
{
male,
Female
}
Sex sex = Sex.male;
Console.WriteLine(sex is Enum);
注意:其他数据类型转换可仿照以上案例。