In C#, the “is” and “as” operators play a crucial role in type checking and casting. Nevertheless, it is essential to understand that these operators serve different purposes and exhibit different behaviors. Here is an in-depth comparison between the two.
Use of “is” operator
The “is” operator is used to verify if an object is suitable for a specific type. It will result in true if the object can be converted to the designated type, and false if not. This operator does not carry out any conversion or casting operations.
Syntax
if (obj is Type)
{
// Code to run if obj belongs to Type.
}
Try to understand it with an example,
object obj = "Omatrixtech";
if (obj is string)
{
Console.WriteLine("It is of string type.");
}
- Upon object initialization, obj is set to the value "Omatrixtech".
- if statement is used to verify if obj is a string.
- The condition confirming that obj is a string returns true.
- The statement "It is of string type." is displayed on the console.
Use of “as” operator
The ‘as’ operator endeavors to convert an object to a designated type. If the conversion is successful, it will return the object as the specified type; however, if the conversion fails (meaning the object is incompatible with the specified type), it will return null. The as operator can solely be used with reference types and nullable types.
Syntax
Type variableType = obj as Type;
if (variableType != null)
{
// Code to run if obj was cast to Type successfully.
}
Try to understand it with an example,
object objResult = "Omatrixtech";
string str = objResult as string;
if (str != null)
{
Console.WriteLine("objResult is effectively converted into a string.");
}
In this instance, the string representation of obj yields the value "Omatrixtech" since obj is of type string. In the event that obj was not a string, str would be null.
Key Differences are:-
1. Purpose
- is: Utilized for verifying if an object belongs to a particular type.
- as: used for converting an object to a specific type.
2. Return Value
- is: Provides a boolean result (true or false).
- as: Yields the object converted to the specified type or null in case of a failed conversion.
3. Type Applicability
- is: Applicable to any type (value types and reference types).
- as: Exclusive to reference types and nullable types.
4. Null Handling
- is: Object remains unchanged, solely verifying its type.
- as: Returns null if the object does not match the specified type.
In C# 7.0 and beyond, it is possible to merge the type check and casting into one statement by utilizing the keyword alongside pattern matching.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp4
{
public class TestOperator
{
public static void GetCombinedOperator()
{
object objResult = "Omatrixtech";
if (objResult is string str)
{
// objResult is now a string
Console.WriteLine(str);
}
}
}
}
Note. The provided syntax verifies whether the object is a string and, if it is, converts it to a string and assigns it to the variable "str".