The difference and use of the IS keyword and as keyword in C #
reprint please indicate from Zhu Zhu Homeland http://blog.csdn.net/zhgl7688
The IS keyword is a type of judgment that checks whether an object is compatible with a given type, does not throw an exception if it is not successful, and returns true if incompatible if it is incompatible. Before you perform a type conversion, use the
if (P_obj is System.String) { String objstr = (string) p_obj; MessageBox.Show ("Compatible with type", "Hint!") "); } else MessageBox.Show ("incompatible with type", "Hint!") ");
In the example above, two compatibility checks are made, one at the time of P_obj is System.String, and the other at the time of conversion (String) p_obj. Use as only once.
The AS keyword is a transformation, you can convert an object to a specified type, and a conversion succeeds, unlike is, to return the converted object, not to throw an exception but to return NULL.
String objstr = P_obj As String; if (objstr! = null) MessageBox.Show ("Compatible with type", "Hint!") "); else MessageBox.Show ("incompatible with type", "Hint!") ");
reprint please indicate from Zhu Zhu Homelandhttp://blog.csdn.net/zhgl7688
The difference and use of the IS keyword and as keyword in C #