【IT168技术文档】
看下面的代码:
这下很清楚了,()和as在做类型转换的时候都是一样处理的,根本不存在所谓那个性能好那个性能坏的问题,完全是受个人习惯影响的。//类型定义 public interface I { string Name { get; set; } } public class A : I { public string Name { get; set; } } public class B : A { public int Key; public static explicit operator string(B operand) { return operand.Name; } } //方法调用 private void button1_Click(object sender, EventArgs e) { B b = new B(); A a1, a2; a1 = (A)b; a2 = b as A; string h = (string)b; } 对应调用方法的IL代码: .method private hidebysig instance void button1_Click(object sender, class [mscorlib]System.EventArgs e) cil managed { .maxstack 1 .locals init ( [0] class WindowsFormsApplication15.B b, [1] class WindowsFormsApplication15.A a1, [2] class WindowsFormsApplication15.A a2, [3] string h) L_0000: nop L_0001: newobj instance void WindowsFormsApplication15.B::.ctor() L_0006: stloc.0 L_0007: ldloc.0 L_0008: stloc.1 L_0009: ldloc.0 L_000a: stloc.2 L_000b: ldloc.0 L_000c: call string WindowsFormsApplication15.B::op_Explicit(class WindowsFormsApplication15.B) L_0011: stloc.3 L_0012: ret }