Javatpoint Logo
Javatpoint Logo

Difference between Is and As operator keyword in C#

The 'is' operator works very effectively when type conversions are not required, and the sole task is verifying an object's type. On the other hand, the 'as' operator works effectively when you must verify an object's type and convert it to a different one if it isn't the provided type. In this article, you will learn about the difference between Is and As operator keywords in C#. But before discussing their differences, you must need to know about the Is and As operator keywords in C#.

What is 'is' Operator?

The 'is' operator is used to accomplish the Type checking in C#. It determines if an object belongs to a given type or a type derived from it. The output of the type check is indicated by the Boolean value (true or false) that the 'is' operator returns.

Syntax:

It has the following syntax:

expression: It will be tested on the specified type.

is: 'is' an operator keyword.

type: The type you want to check against.

Methods of Type Checking:

There are several methods of type checking. Some main methods of type checking are as follows:

1. Basic Type Checking

In C#, basic type checking involves checking if an object is of a certain type or derived from it using the is operator. This feature enables developers to ascertain an object's type at runtime and respond appropriately.

Example:

Let us take an example to illustrate the use of Basic Type Checking in C#.

Output:

O is a string.

Explanation:

  1. The 'O' is allocated an object of type string that has been constructed.
  2. If object 'O' is of type string, it is verified using the is
  3. Since 'O' is indeed a string, the condition is true.
  4. After that, the "O is a string." is printed to the console by the program.

2. Checking for Interface Implementation

In C#, you can use the 'is' operator to check whether an object implements a particular interface. It is a helpful feature when you want to do certain actions if an object adheres to a specific interface.

Example:

Let us take an example to illustrate the Interface Implementation in C#.

Output:

O implements Animal.

Explanation:

  1. We construct and assign to 'O' an object of type
  2. The operator verifies if the object 'O' belongs to the Animal type or a type that is derived from Animal.
  3. The condition holds since the Dog implements the Animal
  4. After that, the program prints to the console "O implements Animal.".

3. Checking for Derived Types

Checking derived types is determined by using the is operator in C#, whether an object is of a given type or any type derived from it in the inheritance hierarchy. It is particularly true when you want to take certain actions depending on the relationship between types.

Example:

Output:

O is an Parent.

Explanation:

  1. The 'O' receives the assignment of an object of type Child.
  2. The is operator determines if the object 'O' belongs to a type Parent or a type that is derived from Parent.
  3. The condition holds because 'O' is of type Child, which derives from Parent.
  4. After that, the program prints "O is a Parent." to the console.

5. Using Pattern Matching

In C#, pattern matching with the is operator enables more expressive and concise code when determining an object's type and extracting data from it. It was designed to make casting and type-checking easier to understand and less prone to errors.

Example:

Output:

O is a string that can be parsed as an int.

Explanation:

  1. If 'O' is a string, it is verified using the is
  2. If so, the 'O' cast to the string value is set to the pattern variable str_ing.
  3. After that, the TryParse condition is examined. The message regarding parsing as an integer is printed if the process is successful; if not, a different message is printed.

What is the 'as' Operator?

The 'as' operator returns a Boolean result (true or false) depending on whether the expression provided may be converted to the given type. It is frequently used in conditional statements or expressions to execute distinct actions depending on the kind of object.

The 'as' operator is used to convert compatible reference or Nullable types. This operator returns the object when it is compatible with the specified type and returns null if the conversion is unsuccessful rather than throwing an exception. The working of as operator is quite similar to 'is' an operator but in a shortening manner.

When using explicit type casting, the 'as' operator is utilised for converting an object into a specific type or a type that is derived from it. If the cast is successful, the result is the casted object; otherwise, the result is null.

Syntax:

It has the following syntax:

expression: An expression is the object you want for your cast.

as: The 'as' an operator keyword

type: The type to which you wish to apply the expression is called type.

1. Checking for Derived Types

The 'as' operator is used to accomplish the casting reference types in C#. If the cast is successful, you can try to check for derived types with the 'as' operator by casting an object to a certain type and getting a reference to the object. If the cast fails, the result is null.

Example:

Let us take an example to check for derived type in C#.

Output:

Oops!!

Explanation:

  1. In this example, we construct and assign to 'O' an object of type Child.
  2. The operator tries to cast 'O' to Child type.
  3. The conditional statement determines if the casting was successful.
  4. If it is successful, it invokes the Child class's Display() method.
  5. If it is unsuccessful, it prints a message indicating that the object is not Child type.
  6. This program will output an "Oops!!," indicating that the cast was successful and that the Child class's Display() method was invoked.

2. Handling Arrays

You may use the 'as' operator when casting an array from a base type to a derived. It is commonly used for casting reference types. However, it is crucial to remember that the 'as' operator only functions with reference and nullable types and cannot be used with other kinds.

Example:

Output:

The string found is: Joe

Explanation:

  1. In this example, the foreach loop iterates over every element in the HetroArray.
  2. After that, the as operator tries to cast each element to a string.
  3. If it works, the "Console.WriteLine" command is run, handling the element as a string.
  4. The element is skipped if it fails.
  5. This program's output will be:
  6. The string found is: Joe
  7. The output confirms that the operator correctly converted the array's first member, a string, to a string type and that the program accordingly printed a message.

3. Checking for Interface Implementation

When casting reference types in C#, the 'as' operator can be used to verify interface implementation. When the 'as' operator successfully casts an object to a given type, it produces the object of that type; if not, it returns 'null'. When an object implements the interface, the result of the check for interface implementation is a non-null reference to the object; if not, the result is 'null'.

Example:

Output:

Helloo...

Explanation:

  1. In this example, the 'O' is assigned to an object of type Animal that has been created.
  2. After that, the operator attempts to cast 'O' to represent the type of Dog.
  3. The conditional statement determines if the casting was successful.
  4. If it is successful, it invokes the Dog interface's Display() method.
  5. It prints a message stating that the object does not implement Dog if unsuccessful.
  6. This program's output will be:
  7. Hello...
  8. The Display() method of the Dog interface was called, and the as operator successfully cast the Animal object to the Dog interface, as this output demonstrates.

4. Basic Type Conversion

The 'as' operator is frequently used in C# for basic type conversion, particularly casting reference types. When the 'as' operator succeeds in casting an object to a given type, it produces the object of that type; if not, it returns 'null'. It's crucial to remember that the as operator can only be used with nullable value types and reference types.

Example:

Output:

Casting successful: Hii, Joe!!
  1. In this example, the 'O' is allocated an object of type string that has been constructed.
  2. After that, a cast of 'O' to type string is attempted through the as operator.
  3. The conditional statement determines if the casting was successful.
  4. The cast string is printed to the console if it is successful.
  5. It prints a message stating that the casting failed if it is unsuccessful.
  6. Output: This program's output will be:
  7. Casting successful: Hii, Joe!!
  8. This output confirms that the object was correctly cast to a string using the 'as' operator and that the program printed the cast string.

Note: It's vital to remember that an "if" statement can use the IS operator, and a "try-catch" statement can use the As operator.

Key difference between Is and As operator keyword in C#

Difference between Is and As operator keyword in C#

There are several differences between the Is and As Operator in C#. Some main differences between the Is and As operator are as follows:

Category is Operator as Operator
Purpose Type checking Type checking
Syntax expression is type expression as type
Result Returns false or true It returns null or the object of the specified type.
Behaviour Does not cast instead, it verifies compatibility Performs an actual cast; if the cast fails, returns null.
Safety Secure for both value types and reference types. Safe with nullable value types; applicable to reference types as well.
Exception Handling No risk of exceptions Does not raise exceptions; returns null in case of failure.
Usage Used for type checking in conditional statements. Used to manage potential failures and for safe type casting.
Typical Scenario Checking the type of an object before type-specific operations. Casting an object to a specific type without exceptions.
  1. The 'as' operator is limited to reference types. It cannot be used with value types such as float or int.
  2. Value and reference types are both compatible with the is operator.
  3. As the 'as' operator only has to perform the cast once, it is more efficient than the 'is' operator.
  4. As we can use the 'is' operator in a wider range of situations than the 'as' operator, it is more flexible.
  5. The 'as' operator returns a valid cast or null, but the IS operator returns a Boolean value. It is the main distinction between the two operators. In other words, the is operator checks an object's type while the as operator checks its castability.

Advantages of AS over IS Operator

Although each operator has its applications, there are some circumstances in which the AS operator is better than the IS operator.

  1. The code is more understandable and concise when the as operator is used instead of an explicit cast operator.
  2. By using the as operator, casts may be carried out safely and without the risk of runtime errors, which can happen when using an explicit cast operator.
  3. If the cast is invalid, the as operator allows null values to be returned, avoiding additional error handling code.

Conclusion:

In conclusion, the C# developer's tools are incomplete without the is and as operators. Although they have certain commonalities, certain distinctions also make them beneficial in various situations. While the as operator offers a secure method for casting, the is operator is helpful for type verification.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news