using System; class Class1 { } class Class2 { }
public class Sample {
public static void Test(object o)
{
if (o is Class1)
Console.WriteLine ("c1是Class1");
else if (o is Class2)
Console.WriteLine ("c2是Class2");
else
Console.WriteLine ("{0}既不是Class1,也不是Class2",o);
}
public static void Main() { Class1 c1 = new Class1(); Class2 c2 = new Class2(); Test(c1); Test(c2); Test("a string"); }
Is运算符是用来判断的,判断类型是否为其值为某个类型或其父类型返回true或false.
as 运算符用于执行可兼容类型之间的转换,转换失败则返回null
比如比如 string s =obj as string;
|