IS Operator

Why Trust Techopedia

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

Margaret Rouse
Editor

Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…