class ClassA : ICloneable
{
public object Clone()
{
return this.MemberwiseClone();
}
public override string ToString()
{
return "It is ClassA";
}
}
class ClassB : ClassA
{
public override string ToString()
{
return "It is ClassB";
}
}
class ClassC : ClassB
{
public override string ToString()
{
return "It is ClassC";
}
}
as 的用法
object[] objArray = new object[6];
objArray[0] = new ClassA();
objArray[1] = new ClassB();
objArray[2] = "hello";
objArray[3] = 123;
objArray[4] = 123.4;
objArray[5] = null;
for (int i = 0; i < objArray.Length; ++i)
{
string s = objArray[i] as string;
Console.Write("{0}:", i);
if (s != null)
{
Console.WriteLine("'" + s + "'");
}
else
{
Console.WriteLine("not a string");
}
}
0:not a string
1:not a string
2:'hello'
3:not a string
4:not a string
5:not a string
Press any key for continuing...
is 的用法
var cl1 = new ClassA();
Console.WriteLine($"cl1 is IFormatProvider : {cl1 is IFormatProvider}");
Console.WriteLine($"cl1 is ICloneable : {cl1 is ICloneable}");
Console.WriteLine($"cl1 is Object : {cl1 is Object}");
Console.WriteLine($"cl1 is ClassA : {cl1 is ClassA}");
Console.WriteLine($"cl1 is ClassB : {cl1 is ClassB}");
Console.WriteLine($"cl1 is ClassC : {cl1 is ClassC}");
Console.WriteLine($"Press any key for continuing...{Environment.NewLine}");
Console.ReadKey();
var cl2 = new ClassB();
Console.WriteLine($"cl2 is IFormatProvider : {cl2 is IFormatProvider}");
Console.WriteLine($"cl2 is ICloneable : {cl2 is ICloneable}");
Console.WriteLine($"cl2 is Object : {cl2 is Object}");
Console.WriteLine($"cl2 is ClassA : {cl2 is ClassA}");
Console.WriteLine($"cl2 is ClassB : {cl2 is ClassB}");
Console.WriteLine($"cl2 is ClassC : {cl2 is ClassC}");
Console.WriteLine($"Press any key for continuing...{Environment.NewLine}");
Console.ReadKey();
var cl3 = new ClassC();
Console.WriteLine($"cl3 is IFormatProvider : {cl3 is IFormatProvider}");
Console.WriteLine($"cl3 is ICloneable : {cl3 is ICloneable}");
Console.WriteLine($"cl3 is Object : {cl3 is Object}");
Console.WriteLine($"cl3 is ClassA : {cl3 is ClassA}");
Console.WriteLine($"cl3 is ClassB : {cl3 is ClassB}");
Console.WriteLine($"cl3 is ClassC : {cl3 is ClassC}");
Console.WriteLine($"Press any key for continuing...{Environment.NewLine}");
Console.ReadKey();
cl1 is IFormatProvider : False
cl1 is ICloneable : True
cl1 is Object : True
cl1 is ClassA : True
cl1 is ClassB : False
cl1 is ClassC : False
Press any key for continuing...
cl2 is IFormatProvider : False
cl2 is ICloneable : True
cl2 is Object : True
cl2 is ClassA : True
cl2 is ClassB : True
cl2 is ClassC : False
Press any key for continuing...
cl3 is IFormatProvider : False
cl3 is ICloneable : True
cl3 is Object : True
cl3 is ClassA : True
cl3 is ClassB : True
cl3 is ClassC : True
Press any key for continuing...
Console.WriteLine($"ClassA clxl = cl2;");
ClassA clx1 = cl2;
Console.WriteLine($"ClassB clx2 = cl3;");
ClassB clx2 = cl3;
Console.WriteLine($"ClassA clx3 = cl3;");
ClassA clx3 = cl3;
Console.WriteLine($"{Environment.NewLine}");
Console.WriteLine($"clx1 is ClassA : {clx1 is ClassA}");
Console.WriteLine($"clx1 is ClassB : {clx1 is ClassB}");
Console.WriteLine($"clx1 is ClassC : {clx1 is ClassC}");
Console.WriteLine($"clx2 is ClassA : {clx2 is ClassA}");
Console.WriteLine($"clx2 is ClassB : {clx2 is ClassB}");
Console.WriteLine($"clx2 is ClassC : {clx2 is ClassC}");
Console.WriteLine($"clx3 is ClassA : {clx3 is ClassA}");
Console.WriteLine($"clx3 is ClassB : {clx3 is ClassB}");
Console.WriteLine($"clx3 is ClassC : {clx3 is ClassC}");
Console.WriteLine($"Press any key for continuing...{Environment.NewLine}");
Console.ReadKey();
ClassA clxl = cl2;
ClassB clx2 = cl3;
ClassA clx3 = cl3;
clx1 is ClassA : True
clx1 is ClassB : True
clx1 is ClassC : False
clx2 is ClassA : True
clx2 is ClassB : True
clx2 is ClassC : True
clx3 is ClassA : True
clx3 is ClassB : True
clx3 is ClassC : True
Press any key for continuing...
類型模式執行模式比對
var cl1 = new ClassA();
var cl2 = new ClassB();
var cl3 = new ClassC();
object MyTest;
MyTest = cl2;
if (MyTest is ClassC e1)
{
Console.WriteLine($"cl2 is ClassC : {e1.ToString()}");
}
if (MyTest is ClassA e2)
{
Console.WriteLine($"cl2 is ClassA : {e2.ToString()}");
}
Console.WriteLine($"Press any key for continuing...{Environment.NewLine}");
Console.ReadKey();
cl2 is ClassA : It is ClassB
Press any key for continuing...
Press any key for continuing...