>>> protected override void OnTextChanged(EventArgs e) >>> { >>> if (this.Parent is IMyInterface) >>> { >>> ((IMyInterface)this.Parent).MyMethod(true); >>> } >>> base.OnTextChanged(e); >>> } >>>>
>>> protected override void OnTextChanged(EventArgs e) >>> { >> IMyInterface o; >>> if ( ( o = this.Parent as IMyInterface) != null ) >>> { >>> o.MyMethod(true); >>> } >>> base.OnTextChanged(e); >>> } >>>
isinst BaseTest.IMyInterfaceThe IS has an additional cast
IL_001d: castclass BaseTest.IMyInterface
public static void WithIs() { var o = new Regex("p"); if (o is IMyInterface) ((IMyInterface)o).MyMethod(false); } // Code size 42 (0x2a) .maxstack 2 .locals init ([0] class [System]System.Text.RegularExpressions.Regex o, [1] bool CS$4$0000) IL_0000: nop IL_0001: ldstr "p" IL_0006: newobj instance void [System]System.Text.RegularExpressions.Regex::.ctor(string) IL_000b: stloc.0 IL_000c: ldloc.0 IL_000d: isinst BaseTest.IMyInterface IL_0012: ldnull IL_0013: cgt.un IL_0015: ldc.i4.0 IL_0016: ceq IL_0018: stloc.1 IL_0019: ldloc.1 IL_001a: brtrue.s IL_0029 IL_001c: ldloc.0 IL_001d: castclass BaseTest.IMyInterface IL_0022: ldc.i4.0 IL_0023: callvirt instance void BaseTest.IMyInterface::MyMethod(bool) IL_0028: nop IL_0029: ret } // end of method test2::WithIs
public static void WithAs() { var o = new Regex("p"); IMyInterface xx; if ((xx = o as IMyInterface) != null) xx.MyMethod(false); } // Code size 36 (0x24) .maxstack 2 .locals init ([0] class [System]System.Text.RegularExpressions.Regex o, [1] class BaseTest.IMyInterface xx, [2] bool CS$4$0000) IL_0000: nop IL_0001: ldstr "p" IL_0006: newobj instance void [System]System.Text.RegularExpressions.Regex::.ctor(string) IL_000b: stloc.0 IL_000c: ldloc.0 IL_000d: isinst BaseTest.IMyInterface IL_0012: dup IL_0013: stloc.1 IL_0014: ldnull IL_0015: ceq IL_0017: stloc.2 IL_0018: ldloc.2 IL_0019: brtrue.s IL_0023 IL_001b: ldloc.1 IL_001c: ldc.i4.0 IL_001d: callvirt instance void BaseTest.IMyInterface::MyMethod(bool) IL_0022: nop IL_0023: ret } // end of method test2::WithAsFinally - I have done some testing
WithLoop() : 00:00:02.52 WithIs() : 00:00:11.35 WithAs() : 00:00:07.15Is is roughly 9 secs and as around 5 sec = QED
namespace BaseTest { public class test11 { public static void Main(string[] args) { var pp = new test2(); pp.o = new test2(); ExecuteTimed.Run(pp.WithLoop, false); ExecuteTimed.Run(pp.WithIs, false); ExecuteTimed.Run(pp.WithAs, true); } } public interface IMyInterface { void MyMethod(bool b); } public class test2 : IMyInterface { const int nTimes = 1000000000; public object o; //_____________________________________________________________________ public void MyMethod(bool x) { } //______________________________________________________________________ public void WithLoop() { for (int i = nTimes; --i != 0; ) ; } public void WithIs() { IMyInterface xx ; for( int i = nTimes; --i != 0; ) if (o is IMyInterface) { xx = (IMyInterface)o; // xx.MyMethod(false); } } //______________________________________________________________________ public void WithAs() { IMyInterface xx; for (int i = nTimes; --i != 0; ) if ((xx = o as IMyInterface) != null) ; // xx.MyMethod(false); } } //______________________________________________________________________ }