Info about casting
-
Hello I would like to know difference betwen casting and if there is one, then in which conditions shroud be used Example: TextBox txt = new TextBox; Object obj = txt; // What is the difference between these 2 castings TextBox txt1 = (TextBox) txt; TextBox txt2 = txt as TextBox; This may be stupid question but thanks in advance
-
Hello I would like to know difference betwen casting and if there is one, then in which conditions shroud be used Example: TextBox txt = new TextBox; Object obj = txt; // What is the difference between these 2 castings TextBox txt1 = (TextBox) txt; TextBox txt2 = txt as TextBox; This may be stupid question but thanks in advance
-
And in general, I prefer using 'as', but it depends on your situation. If you want an exception to be thrown, use explicit casting (ex. "
((TextBox)obj)
"). Generally, its good to avoid exceptions unless there's just nothing you can do to avoid the 'exceptional' behavior. -
And in general, I prefer using 'as', but it depends on your situation. If you want an exception to be thrown, use explicit casting (ex. "
((TextBox)obj)
"). Generally, its good to avoid exceptions unless there's just nothing you can do to avoid the 'exceptional' behavior.Thanks to both replies. Those answer extinguish my curiosty
-
Thanks to both replies. Those answer extinguish my curiosty