Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IMyInterface
- {
- void AMethod();
- }
- public class MyClass : IMyInterface
- {
- public void AMethod()
- {
- //Do work
- }
- // Other helper methods....
- }
- public class Implementation
- {
- IMyInterface _MyObj;
- MyClass _myCls1;
- MyClass _myCls2;
- public Implementation()
- {
- _MyObj = new MyClass();
- // What is the difference here:
- _myCls1 = (MyClass)_MyObj;
- _myCls2 = (_MyObj as MyClass);
- }
- }
- // Bad code - checks type twice for no reason
- if (randomObject is TargetType)
- {
- TargetType foo = (TargetType) randomObject;
- // Do something with foo
- }
- // This will throw an exception if randomObject is non-null and
- // refers to an object of an incompatible type. The cast is
- // the best code if that's the behaviour you want.
- TargetType convertedRandomObject = (TargetType) randomObject;
- TargetType convertedRandomObject = randomObject as TargetType;
- if (convertedRandomObject != null)
- {
- // Do stuff with convertedRandomObject
- }
- TargetType? convertedRandomObject = randomObject as TargetType?;
- if (convertedRandomObject != null)
- {
- // Do stuff with convertedRandomObject.Value
- }
- using System;
- using System.Diagnostics;
- using System.Linq;
- class Test
- {
- const int Size = 30000000;
- static void Main()
- {
- object[] values = new object[Size];
- for (int i = 0; i < Size - 2; i += 3)
- {
- values[i] = null;
- values[i + 1] = "x";
- values[i + 2] = new object();
- }
- FindLengthWithIsAndCast(values);
- FindLengthWithIsAndAs(values);
- FindLengthWithAsAndNullCheck(values);
- }
- static void FindLengthWithIsAndCast(object[] values)
- {
- Stopwatch sw = Stopwatch.StartNew();
- int len = 0;
- foreach (object o in values)
- {
- if (o is string)
- {
- string a = (string) o;
- len += a.Length;
- }
- }
- sw.Stop();
- Console.WriteLine("Is and Cast: {0} : {1}", len,
- (long)sw.ElapsedMilliseconds);
- }
- static void FindLengthWithIsAndAs(object[] values)
- {
- Stopwatch sw = Stopwatch.StartNew();
- int len = 0;
- foreach (object o in values)
- {
- if (o is string)
- {
- string a = o as string;
- len += a.Length;
- }
- }
- sw.Stop();
- Console.WriteLine("Is and As: {0} : {1}", len,
- (long)sw.ElapsedMilliseconds);
- }
- static void FindLengthWithAsAndNullCheck(object[] values)
- {
- Stopwatch sw = Stopwatch.StartNew();
- int len = 0;
- foreach (object o in values)
- {
- string a = o as string;
- if (a != null)
- {
- len += a.Length;
- }
- }
- sw.Stop();
- Console.WriteLine("As and null check: {0} : {1}", len,
- (long)sw.ElapsedMilliseconds);
- }
- }
- int value;
- if (int.TryParse(text, value))
- {
- value = int.Parse(text);
- // Use value
- }
- _myCls2 = _myObj is MyClass ? (MyClass)_myObj : null;
- public class MyClass
- {
- public static void Main()
- {
- // Call the 2 methods
- }
- public void DirectCast(Object obj)
- {
- if ( obj is MyClass)
- {
- MyClass myclass = (MyClass) obj;
- Console.WriteLine(obj);
- }
- }
- public void UsesAs(object obj)
- {
- MyClass myclass = obj as MyClass;
- if (myclass != null)
- {
- Console.WriteLine(obj);
- }
- }
- }
- .method public hidebysig instance void DirectCast(object obj) cil managed
- {
- // Code size 22 (0x16)
- .maxstack 8
- IL_0000: ldarg.1
- IL_0001: isinst MyClass
- IL_0006: brfalse.s IL_0015
- IL_0008: ldarg.1
- IL_0009: castclass MyClass
- IL_000e: pop
- IL_000f: ldarg.1
- IL_0010: call void [mscorlib]System.Console::WriteLine(object)
- IL_0015: ret
- } // end of method MyClass::DirectCast
- .method public hidebysig instance void UsesAs(object obj) cil managed
- {
- // Code size 17 (0x11)
- .maxstack 1
- .locals init (class MyClass V_0)
- IL_0000: ldarg.1
- IL_0001: isinst MyClass
- IL_0006: stloc.0
- IL_0007: ldloc.0
- IL_0008: brfalse.s IL_0010
- IL_000a: ldarg.1
- IL_000b: call void [mscorlib]System.Console::WriteLine(object)
- IL_0010: ret
- } // end of method MyClass::UsesAs
- public class Foo
- {
- public string Value;
- public static explicit operator string(Foo f)
- {
- return f.Value;
- }
- }
- public class Example
- {
- public void Convert()
- {
- var f = new Foo();
- f.Value = "abc";
- string cast = (string)f;
- string tryCast = f as string;
- }
- }
- if (randomObject is TargetType)
- {
- TargetType foo = randomObject as TargetType;
- // Do something with foo
- }
- TargetType convertedRandomObject = randomObject as TargetType;
- if (convertedRandomObject != null)
- {
- // Do stuff with convertedRandomObject
- }
- IMyInterface = (IMyInterface)someobj;
Add Comment
Please, Sign In to add comment