(MyClassName)object vs. object as MyClassName
- by Matthew Doyle
Hello all, I was wondering what is the better method for Casting objects for C#:
MyClassName test = (MyClassName)object;
MyClassName test = object as MyClassName;
I know already that if you do the first way, you get an exception, and the second way it sets test as null. However, I was wondering why do one over the other? I see the first way a lot, but I like how the second way because then I can check for null...
If there isn't a 'better way' of doing it, what are the guidelines for using one way or the other?