在本文中,我们展示了如何使用typeof
和is
运算符以及GetType
方法在C#中检查类型。
在C#中,每个变量和表达式都有一个类型。类型是一组值和对这些值的允许操作。
类型存储以下信息:
- 该类型的变量需要的存储空间
- 该类型的最大值和最小值
- 字段或方法等类型成员
- 它继承的基类型
- 实现的接口
- 允许的操作
编译器使用这些信息来确保类型安全的操作。
System.Type
类表示类型声明。
我们可以使用typeof
运算符、is
运算符或GetType
方法检查类型。typeof
运算符获取类型的System.Type
实例。运算符在编译时检查类型。它仅适用于类型,不适用于变量。
GetType
方法获取当前对象实例的类型。它在运行时检查类型。
is
运算符检查实例是否在类型的继承树中。此外,它还允许类型安全的转换。
C#typeof示例
第一个示例使用typeof
运算符为内置int类型打印System.Type
。
Type t = typeof(int); Console.WriteLine(t); Console.WriteLine(typeof(List<int>)); Console.WriteLine(typeof(string)); Console.WriteLine(typeof(double)); Console.WriteLine(typeof(float)); Console.WriteLine(typeof(decimal)); Console.WriteLine(typeof(User)); record User(string Name, string Occupation);
该示例为int、List、string、double、float、decimal和User打印System.Type
。
$ dotnet run System.Int32 System.Collections.Generic.List`1[System.Int32] System.String System.Double System.Single System.Decimal User
C#GetType示例
以下示例检查三个变量的类型。
int x = 34; string word = "falcol"; decimal i = 23.54m; if (x.GetType() == typeof(int)) { Console.WriteLine("x has int type"); } if (word.GetType() == typeof(string)) { Console.WriteLine("word has string type"); } if (i.GetType() == typeof(decimal)) { Console.WriteLine("i has decimal type"); }
我们定义了整数、字符串和小数变量。我们使用GetType
方法在运行时检查类型。
$ dotnet run x has int type word has string type i has decimal type
C#是运算符
is
运算符检查对象是否与给定类型兼容,即它是否在其继承树中。
Base _base = new Base(); Derived derived = new Derived(); Console.WriteLine(_base is Base); Console.WriteLine(_base is Object); Console.WriteLine(derived is Base); Console.WriteLine(_base is Derived); class Base { } class Derived : Base { }
我们从用户定义的类型创建两个对象。
class Base {} class Derived : Base {}
我们有一个Base
和一个Derived
类。Derived
类继承自Base
类。
Console.WriteLine(_base is Base); Console.WriteLine(_base is Object);
Base
等于Base
所以第一行打印True。Base
也兼容Object
类型.这是因为每个类都继承自所有类的母体-Object
类。
Console.WriteLine(derived is Base); Console.WriteLine(_base is Derived);
派生对象与Base
类兼容,因为它明确地继承自Base
类。另一方面,_base
对象与Derived
类无关。
$ dotnet run True True True False
C#类型安全检查是
我们可以使用is
运算符执行类型安全转换。
object[] vals = new object[] { 12, "falcon", 3, 1, true, 20 }; foreach (var e in vals) { if (e is int val) { Console.WriteLine($"{val} powered is {val * val}"); } }
我们有一个对象数组。对于所有整数,我们计算它的幂。
if (e is int val) { Console.WriteLine($"{val} powered is {val * val}"); }
如果可以进行转换,is
运算符会创建一个int
类型的局部变量val
。
$ dotnet run 12 powered is 144 3 powered is 9 1 powered is 1 20 powered is 400
或者,我们可以使用LINQ的OfType
方法来完成这项工作。
object[] vals = new object[] { 12, "falcon", 3, 1, true, 20 }; var res = vals.OfType<int>(); foreach (var e in res) { Console.WriteLine($"{e} powered is {e * e}"); }
OfType
方法根据指定类型过滤IEnumerable
的元素。
C#复选框值
使用is
运算符,我们可以检查装箱值的实际类型。装箱是将值类型转换为类型对象
的过程。
object o = 12; Console.WriteLine(o is int); Console.WriteLine(o is double); object o2 = "falcon"; Console.WriteLine(o2 is string); Console.WriteLine(o2 is char);
该示例确定两个装箱值的实际类型。
$ dotnet run True False True False
C#switch表达式类型模式
类型可以是switch表达式的模式。
int age = 23; string name = "Peter"; List<string> colors = new List<string> {"blue", "khaki", "orange"}; int[] nums = new int[] {1, 2, 3, 4, 5}; Console.WriteLine(check(age)); Console.WriteLine(check(name)); Console.WriteLine(check(colors)); Console.WriteLine(check(nums)); object check(object val) => val switch { int => "integer", string => "string", List<string> => "list of strings", Array => "array", _ => "unknown" };
在示例中,我们使用switch表达式检查变量的类型。
$ dotnet run integer string list of strings array
在本文中,我们展示了如何使用typeof
、is
和GetType
在C#中检查类型。
列出所有C#教程。