is就是处于对类型的判断。返回true和false。如果一个对象是某个类型或是其父类型的话就返回为true,否则的话就会返回为false。
is操作符永远不会抛出异常
如果对象引用为null,那么is操作符总是返回为false,因为没有对象可以检查其类型,
class Program
{
static void Main(string[] args)
{
B b=new B();
b.sum("44"); //string
b.sum(222); //other
ReadKey();
}
}
public abstract class A
{
public abstract void sum(object a);
}
public class B : A
{
public override void sum(object a)
{
if (a is string)
{
Console.WriteLine("string");
}
else
{
Console.WriteLine("other");
}
}
}