Definition - What does IS Operator mean?
The IS operator, in C#, is an operator used to dynamically check whether an object is compatible with a given type.
While working with reference types involving base and derived types, it becomes essential to cast the object of base type to derived type and then call the method. In case the simple cast from base to derived type is not successful, it results in throwing an exception. The is operator is used to test for the success of cast operation without causing an exception. It is an efficient operator. It not only checks for the safe and successful cast of an object, but also fetches the cast value on a successful cast. However, it is used often in the code to determine the type of an object without any requirement for cast operation.
Since the is operator always returns Boolean value (true or false) after the cast operation and never throws an exception, it is highly reliable to be used in an application code. In addition, it allows verifying the type of an object with a given type in a simpler manner, which reduces the code size and development effort.
Techopedia explains IS Operator
The is operator is a type testing operator with two parameters, which include an expression of a reference type. The reference type must be checked for conversion and the destination type to which the expression has to be cast. The is operator returns true if the given expression representing the object is not null and the expression succeeds in casting to the specified type. Otherwise, it returns false.
The is operator cannot be overloaded to provide any other semantics. It can be used for checking reference, boxing and unboxing conversions, but not for user-defined conversions. Although the type compatibility of the expression is evaluated at runtime, the C# compiler generates a warning during compilation when the is operator is used on types that are not compatible.
For example, the is operator can be used to check whether the parameter passed to a method is of type, Student or its derived type where Student is a type used to represent the information related to student in an application.
The is operator differs from the AS operator in that the former results in doing two casts, while the latter avoids the double cast to single step and hence better performance is achieved by using the as operator.
Join thousands of others with our weekly newsletter
The 4th Era of IT Infrastructure: Superconverged Systems:
Approaches and Benefits of Network Virtualization:
Free E-Book: Public Cloud Guide:
Free Tool: Virtual Health Monitor:
Free 30 Day Trial – Turbonomic: