Safely cast using pattern matching

C# CONCEPTS

The article clarifies the difference between “is” & “as” operators and helps consolidate them into applications as efficiently as possible.

Learning Objectives

  • How to use the “is” operator
  • How to use the “as” operator

Prerequisites

  • Install Visual Studio or Visual Studio Code.
  • Knowledge about basic C# concepts.

Getting Started

Let’s use the latter code as an example.

Test s = new Test();
object c = s;
string r = (string)c;

If we have an empty class called Test, and if we drafted this code in an IDE such as Visual Studio or Visual Studio Code, there is no compilation error. Following runtime exceptions will occur.

System.InvalidCastException: 'Unable to cast object of type 'Test' to type 'System.String'

One way is to use the try/catch block to handle the above exception. But the better way to handle this exception is using “is” & “as” operators.

C# gives the is and as operators to test if a value is of a particular type.

Is Operator

The “is” operator verifies if the result of the expression is agreeable with a given type by directly testing an expression versus a pattern. It is also called the type testing operator and investigates whether the runtime type of an expression’s result fits a given type. The test against practices came to C# version 7.0.

Example

public class Test{ }
//Use is operator
static void TestClass(object o)
{
if(o is Test){}
}

The preceding example illustrates few features of pattern matching syntax. The if (o is Test t) statement consolidates the Test with an initialization assignment.

The assignment happens only when the Test succeeds. You cannot access “t” later in the method.

As Operator

The “as” operator is applied to produce translations between suitable types. It is very similar to the “is” operator; however, it works uniquely under the hood.

Example

object[] Tests = new object[4];
Tests[0] = new Test();
Tests[1] = new Test();
Tests[2] = new Test();
Tests[3] = new Test();
for(int i = 0; i < 4; i++)
{
string s = Tests[i] as string;
}

Iterate across the array, checking which one is compatible with the string type with the aid of the as operator.

Summary “is” operator.

  • The “is” operator is used to verify if the type of object fits with the given type or not.
  • It is of boolean type.
  • It returns true if a given object is of the same type.
  • It returns false if a given object is not of the same type.
  • Utilized for reference, boxing, and unboxing.

Summary “as” operator

  • The “as” operator is applied to produce translations between suitable types.
  • It is of the object type.
  • It returns an object if a given object is of the same type.
  • It returns null if a given object is not of the same type.
  • Utilized for reference, boxing, and nullable.

Summary

Both operators consolidate more superior flexibility into the applications and allow to development of more robust programs.


Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section.

Follow me on LinkedIn, Instagram Facebook Twitter

Leave a comment