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 运算符来判断对象是否是这两个类的实例。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:typeof c#如何定义自定义类型