IS Operator

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.

Advertisements

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.

Advertisements

Related Terms

Latest DevOps Terms

Related Reading

Margaret Rouse

Margaret Rouse is an award-winning technical writer and teacher known for her ability to explain complex technical subjects to a non-technical, business audience. Over the past twenty years her explanations have appeared on TechTarget websites and she's been cited as an authority in articles by the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine and Discovery Magazine.Margaret's idea of a fun day is helping IT and business professionals learn to speak each other’s highly specialized languages. If you have a suggestion for a new definition or how to improve a technical explanation, please email Margaret or contact her…