The Is and As Operators in C#

Introduction

 
Look at the example given below:
  1. Circle c = new Circle(32);  
  2. object o = c;  
  3. int i = (int)o; // it compiles okay but throws an exception at runtime  
Here the runtime is more suspicious; if the type of object in memory does not match the cast, the runtime will throw an InvalidCastException. We should be prepared to catch this exception and handle it appropriately if it occurs. However, catching an exception and attempting to recover in the event that the type of an object is not what we expected it to be is a rather cumbersome approach. C# provides two more very useful operators that can help us to perform casting in a much more elegant manner by using the "is" and "as" operators. Let's have some discussion of the operators.
 

is Operator

 
The "is" operator is used to check whether the run-time type of an object is compatible with a given type or not. In other words, we use the "is" operator to verify that the type of an object is what we expect it to be. Let's look at its syntax: 
 
expression is type 
 
Example of the "is" operator:
  1. using System;  
  2. class Class1  
  3. {  
  4. }  
  5. class Class2  
  6. {  
  7. }  
  8. public class IsTest  
  9. {  
  10.     public static void Test(object o)  
  11.     {  
  12.         Class1 a;  
  13.         Class2 b;  
  14.         if (o is Class1)  
  15.         {  
  16.             Console.WriteLine("o is Class1");  
  17.             a = (Class1)o;  
  18.         }  
  19.         else if (o is Class2)  
  20.         {  
  21.             Console.WriteLine("o is Class2");  
  22.             b = (Class2)o;  
  23.         }  
  24.         else  
  25.         {  
  26.             Console.WriteLine("o is neither Class1 nor Class2.");  
  27.         }  
  28.     }  
  29.     public static void Main()  
  30.     {  
  31.         Class1 c1 = new Class1();  
  32.         Class2 c2 = new Class2();  
  33.         Test(c1);  
  34.         Test(c2);  
  35.         Test("Passing String Value instead of class");  
  36.         Console.ReadKey();  
  37.     }  
  38. }
is Operator in C# 
 
In the above example, I'll be checking whether object o is a class or not. If the argument passed is not a class then the application will jump to the message 'o is neither class1 nor class2'. 
 

as Operator 

 
The "as" operator is used to perform conversions between compatible types. Actually, the "as" operator fulfills a similar role like "is" but in a slightly truncated manner. Let's look at its syntax: 
 
expression as type 
 
Example of the "as" operator:
  1. using System;  
  2. class Class1  
  3. {  
  4. }  
  5. class Class2  
  6. {  
  7. }  
  8. public class IsTest  
  9. {  
  10.     public static void Main()  
  11.     {  
  12.         object[] myObjects = new object[6];  
  13.         myObjects[0] = new Class1();  
  14.         myObjects[1] = new Class2();  
  15.         myObjects[2] = "string";  
  16.         myObjects[3] = 32;  
  17.         myObjects[4] = null;  
  18.         for (int i = 0; i < myObjects.Length; ++i)  
  19.         {  
  20.             string s = myObjects[i] as string;  
  21.             Console.Write("{0}:", i);  
  22.             if (s != null)  
  23.                 Console.WriteLine("'" + s + "'");  
  24.             else  
  25.                 Console.WriteLine("not a string");  
  26.         }  
  27.         Console.ReadKey();  
  28.     }  
  29. }
as Operator in C#
 
In the above example, each and every value is being cast to a string using the "as" operator and assigned to a string variable which is shown on the console.
 
So, that's all about the "is" and "as" operators. Thanks for joining this article.
 
HAVE A HAPPY CODING!!


Similar Articles