Consider:
and:Code:Control c = new Control();
object o = (object)c;
Control x = (Control)o;
Is either method preferable and if so why?Code:Control c = new Control();
object o = c as object;
Control x = o as Control;
Printable View
Consider:
and:Code:Control c = new Control();
object o = (object)c;
Control x = (Control)o;
Is either method preferable and if so why?Code:Control c = new Control();
object o = c as object;
Control x = o as Control;
The 'as' keyword doesn't simply cast. It will test the type of the actual object first. If the object is of the type being cast to then the cast is performed. If the object is not of that type then a null reference is returned. If you know for sure that the object is of the type that you're casting to then a straight cast is preferable because it is faster. If the types don't match though, an exception will be thrown, so if there is any doubt about the type of the object then use 'as'. Of course, the MSDN library topic for the C# 'as' keyword would have told you all that.
I don't keep a local copy and the web site won't connect.Quote:
Originally Posted by jmcilhinney
Thanks for the help anyway.
You absolutely should. I can't think of any valid reason not to.Quote:
Originally Posted by wey97