Safe Casting
Use the as keyword.
MyClass castedObject = obj as MyClass;
This won’t throw a class cast exception. Instead, castedObject will return null if obj isn’t a MyClass.
Use the as keyword.
MyClass castedObject = obj as MyClass;
This won’t throw a class cast exception. Instead, castedObject will return null if obj isn’t a MyClass.