标签:
1.is:检查对象类型是否兼容制定类型。返回true,false
bool x = a is b;
2.as: 效率高于is
因为B为父类,子类可以转换为父类,故b不等于空,无输出
public class main { public static void Main () { A a =new A (); B b = a as B; if (b == null) { Console.WriteLine ("---"); } } } class A:B{ } class B{ }
与上面情况相反,输出---
public class main { public static void Main () { A a =new A (); B b = a as B; if (b == null) { Console.WriteLine ("---"); } } } class A{ } class B:A{ }
标签:
原文地址:http://www.cnblogs.com/nnnnn/p/4580399.html