To evaluate a reference type variable, simply use "is" keyword. It will tell you whether or not a variable is capable of being cast to another type. If you are not sure about the type of the variable before casting it, you should use "is".
For example if you are iterating through the web controls, you can utilize the following code:
for each (control Ctrl in Page.Form.Controls)
{
if (Ctrl is DropDownList)
DropDownList Ddl =(DropDownList)Ctrl;
}
In the above example, if you don't use "is" keyword, application tries to cast other types of controls to DropDownList and obviously it will throw a cast exception.
No comments:
Post a Comment