C# 支持自定义类型判断,可以通过自定义类或结构体来创建自定义类型,然后使用 is 运算符来判断对象是否是该自定义类型的实例。例如:
public class Person { public string Name { get; set; } public int Age { get; set; } } public class Student : Person { public string School { get; set; } } Person person = new Person(); if (person is Person) { Console.WriteLine("person is a Person"); } Student student = new Student(); if (student is Student) { Console.WriteLine("student is a Student"); }
在上面的示例中,我们定义了一个 Person 类和一个 Student 类,然后使用 is 运算符来判断对象是否是这两个类的实例。