In this blog, I am going to share with you about 'as' and 'is' keywords in C#.
Introduction
'as' and 'is' are the keywords used for conversion/TypeCasting in C#. I hope you have the basic understanding of casting/TypeCasting in C#. If not, don't worry. Just visit the link below to have a clear understanding of it.
Why should we use 'as' keyword?
Let's understand the below program to know why we should use the 'as' keyword.
- using System;
- namespace Tutpoint
- {
- class Program
- {
- class Tutpoint
- {
- public Tutpoint()
- {
- Console.WriteLine("Hello TutPoint");
- }
- }
- static void Main(string[] args)
- {
- // Creating object of object type
- object obj = new object();
- Console.WriteLine(obj);
- // This will raise a runtime InvalidCastException as "Unable to cast object of type 'System.Object' to type 'Tutpoint'"
- Tutpoint tutpoint = (Tutpoint)obj;
- if (tutpoint != null)
- {
- Console.WriteLine("tutpoint does not contain null value");
- }
- else
- {
- Console.WriteLine("tutpoint contains null value");
- }
- Console.ReadKey();
- }
- }
- }
In this program, inside 'Main' method, first, we created an object 'obj' of the object type. Then, what we did is casting 'obj' of type object to 'TutPoint' type. This casting is incompatible and raises a runtime exception as "Unable to cast object of type 'System.Object' to type 'Tutpoint'".
So, in order to avoid runtime exceptions, the 'as' keyword is used. If the casting is incompatible, then it will return a null value instead of an exception. This is very useful in saving our program from crashes/exceptions.
What is 'as' Keyword?
- 'as' is a keyword used for conversion from one type to another. The type can be a reference or nullable.
- 'as' keyword checks the compatibility of one object type with another object type. In case of compatible, it will return the value of the new object type otherwise, null will be returned.
- If the conversion from one type to another type fails, then it will return a null value instead of raising an exception. So, the return value can be null also.
Syntax of 'as' keyword- Expression as dataType;
- We cannot perform conversion of value types (int, double, char, bool) and user-defined types.
- The return type should be the reference or nullable type. Since the returned value can be null and as we know value types cannot contain null, value types cannot be used.
- 'as' improves the performance and it is safe for casting.
The above program can be rewritten using 'as' keyword.
- using System;
- namespace Tutpoint
- {
- class Program
- {
- class Tutpoint
- {
- public Tutpoint()
- {
- Console.WriteLine("Hello TutPoint");
- }
- }
- static void Main(string[] args)
- {
- // Creating object of object type
- object obj = new object();
- Console.WriteLine(obj);
- // 'as' keyword used to convert 'obj' of type 'object' to 'Tutpoint' type
- // It will return null as the conversion of object type to Tutpoint is incompatible
- Tutpoint tutpoint = obj as Tutpoint;
- if (tutpoint != null)
- {
- Console.WriteLine("tutpoint does not contain null value");
- }
- else
- {
- Console.WriteLine("tutpoint contains null value");
- }
- Console.ReadKey();
- }
- }
- }
Output
- System.Object
- tutpoint contains null value
Another example.
- using System;
- namespace Tutpoint
- {
- class Program
- {
- class Tutpoint
- {
- public string Name { get; set; }
- }
- class Tutlover
- {
- public string Name { get; set; }
- }
- static void Main(string[] args)
- {
- //Creating object of classes 'Tutpoint' and 'Tutlover'
- Tutpoint tutpoint = new Tutpoint();
- Tutlover tutlover = new Tutlover();
- //Creating array of objects
- object[] object_array = new object[7];
- object_array[0] = tutpoint;
- object_array[1] = tutlover;
- object_array[2] = "hey..Folks";
- object_array[3] = 1000;
- object_array[4] = 1.212;
- object_array[5] = 'c';
- object_array[6] = true;
- for (int i = 0; i < 7; i++)
- {
- // as-casting of obj_array[i] value to string. If compatible then return the value else return null
- string str = object_array[i] as string;
- if (str != null)
- {
- Console.WriteLine(str);
- }
- else
- {
- Console.WriteLine("Null value");
- }
- }
- Console.WriteLine();
- for (int i = 0; i < 6; i++)
- {
- // as-casting of obj_array[i] value to Tutlover type. If compatible then return the value else return null
- Tutlover obj = object_array[i] as Tutlover;
- if (obj != null)
- {
- Console.WriteLine(obj);
- }
- else
- {
- Console.WriteLine("Null value");
- }
- }
- Console.WriteLine();
- for (int i = 0; i < 6; i++)
- {
- // as-casting of obj_array[i] value to object. If compatible then return the value else return null
- object obj = object_array[i] as object;
- if (obj != null)
- {
- Console.WriteLine(obj);
- }
- else
- {
- Console.WriteLine("Null value");
- }
- }
- Console.ReadKey();
- }
- }
- }

Join the conversation! Your thoughts help the community grow.