The IS and as operators are used for coercion of type conversions.
Is: Checks whether an object is compatible with other specified types and returns a bool value that never throws an exception
Object o = new Object ();
If (O is Label)
{
Label lb = (label) o;
Response.Write ("type conversion succeeded");
}
Else
{
Response.Write ("type conversion failed");
}
In the above code, the CLR will actually check the type of two objects, the IS Operation Mr. Foo Check once, if O is compatible with lable, then the (Label) O will be checked again, the efficiency is low, is not recommended to use
As: is the same as coercion type conversion, but never throws an exception, that is, if the conversion is unsuccessful, NULL is returned
Object o = new Object ();
label lb = o as Label;
if (lb = = null)
{
Response.Write ("type conversion failed");
}
Else
{
Response.Write ("type conversion succeeded");
}
In the above code, the CLR will only perform one type verification, which is more efficient than the IS
CLR via note 4.2 type conversion is and as difference