C# 支持自定義類型判斷,可以通過自定義類或結(jié)構(gòu)體來創(chuàng)建自定義類型,然后使用 is 運(yùn)算符來判斷對(duì)象是否是該自定義類型的實(shí)例。例如:
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");
}
在上面的示例中,我們定義了一個(gè) Person 類和一個(gè) Student 類,然后使用 is 運(yùn)算符來判斷對(duì)象是否是這兩個(gè)類的實(shí)例。