The <, >, <=, >=, =, <>, Is and As operators are named relational and type-testing operators.
relational-expression < additive-expression
relational-expression > additive-expression
relational-expression <= additive-expression
relational-expression >= additive-expression
relational-expression Is type
relational-expression As type
equality-expression:
relational-expression
equality-expression == relational-expression
equality-expression <> relational-expression
The Is operator is used to check if the runtime type of an object is compatible with a given type. The result of the operation E is T, where E is an expression and T is a type, is a logical value indicating whether E can successfully be converted to type T by a reference conversion, a boxing conversion, or an unboxing conversion. The operator is executed as follows:
If the compile-time type of E is the same as T, either an implicit reference conversion or boxing conversion exists from the compile-time type of E to T:
If E is of a reference type, the result is equivalent to comparing E <> Null.
If E is of a value type, the result of the operation is True.
Otherwise, if an explicit reference conversion or unboxing conversion exists from the compile-time type of E to T, a dynamic type check is executed:
If the value of E is Null, the result of the operation is False.
Otherwise, let R be the runtime type of the instance referenced by E. If R and T are the same type, or if R is a reference type and an implicit reference conversion from R to T exists, or if R is a value type and T is an interface type that is implemented by R, the result of the operation is True.
Otherwise, the result of the operation is False.
Otherwise, no reference or boxing conversion of E to type T is possible, and the result of the operation is False.
The As operator is used to explicitly convert a value to a given type. In an operation of the form E As T, E must be an expression and T must be a type. The type of the result is T, and the result is always classified as a value. The operation is executed successfully if an explicit conversion exists from the compile-time type of E to T. Otherwise, System.InvalidCastException is raised.
See also: