In C# What's the difference in casting if I do
MyType mytype = (MyType) obj;
Or
MyType mytype = obj as MyType;
In C# What's the difference in casting if I do
MyType mytype = (MyType) obj;
Or
MyType mytype = obj as MyType;
The first methods fails when obj cannot be converted into MyType. When you use the second way then mytype is eitherobj casted into MyType or it is null if it cannot be casted.
The second one can only be used with reference types (classes and interfaces) and will return null if the variable is not of the cast type. The first works with any types (structs as well) and will instead throw an exception.