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)
- {
-
- object obj = new object();
- Console.WriteLine(obj);
-
-
- 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
- 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)
- {
-
- object obj = new object();
- Console.WriteLine(obj);
-
-
-
- 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)
- {
-
- Tutpoint tutpoint = new Tutpoint();
- Tutlover tutlover = new Tutlover();
-
-
- 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++)
- {
-
- 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++)
- {
-
- 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++)
- {
-
- object obj = object_array[i] as object;
- if (obj != null)
- {
- Console.WriteLine(obj);
- }
- else
- {
- Console.WriteLine("Null value");
- }
- }
- Console.ReadKey();
- }
- }
- }
Output:
- Null value
- Null value
- hey..Folks
- Null value
- Null value
- Null value
- Null value
-
- Null value
- Tutpoint.Program+Tutlover
- Null value
- Null value
- Null value
- Null value
-
- Tutpoint.Program+Tutpoint
- Tutpoint.Program+Tutlover
- hey..Folks
- 1000
- 1.212
- c
In the above program, we created an array of objects and assigned different values to it. After that, we cast it using 'as' keyword to string, Tutlover class type, object type respectively.
When we cast it with string,
- string str = object_array[i] as string;
Then, it will return the string value only when object_array[i] value is of 'string' type, else return null.
When we cast it with Tutlover class type,
- Tutlover str = object_array[i] as Tutlover;
Then, it will return the value of 'Tutlover' type only when object_array[i] value is of 'Tutlover' type, else return null.
When we cast it with object class type,
- object str = object_array[i] as object;
Then, it will return value of any type.
Now, what if we cast it with int, bool, double, char??
- int int_value = object_array[i] as int;
- char char_value = object_array[i] as char;
- double double_value = object_array[i] as double;
- bool bool_value = object_array[i] as bool;
The compiler will generate an error as "The as operator must be used with a reference type or nullable type ('type' is a non-nullable value type)"
What is 'is' keyword?
1) 'is' keyword checks whether the conversion from one object type to another object type is compatible or not.
2) It returns true if the conversion is compatible, else returns false.
3) The syntax of 'is' keyword is,
- Boolean result = object_type_to is object_type_From;
Here, 'object_type_to' is the object type which is to be check with object type 'object_type_From'. 'result' is the variable of bool type.
4) 'is' evaluates compatibility at runtime.
5) 'is' keyword can also check compatibility of value types (int, double, char, bool) and user-defined types.
- 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)
- {
- Tutpoint tutpoint = new Tutpoint();
- Tutlover tutlover = new Tutlover();
-
-
- 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++)
- {
-
- Boolean result = object_array[i] is string;
- if (result)
- {
- Console.WriteLine("True value");
-
- }
- else
- {
- Console.WriteLine("False value");
-
- }
- }
- Console.WriteLine();
-
- for (int i = 0; i < 7; i++)
- {
-
- Boolean result = object_array[i] is object;
- if (result)
- {
- Console.WriteLine("True value");
-
- }
- else
- {
- Console.WriteLine("False value");
-
- }
- }
- Console.WriteLine();
-
- for (int i = 0; i < 7; i++)
- {
-
- Boolean result = object_array[i] is Tutlover;
- if (result)
- {
- Console.WriteLine("True value");
-
- }
- else
- {
- Console.WriteLine("False value");
-
- }
- }
- Console.WriteLine();
- Console.ReadKey();
- }
- }
- }
Output
- False value
- False value
- True value
- False value
- False value
- False value
- False value
-
- True value
- True value
- True value
- True value
- True value
- True value
- True value
-
- False value
- True value
- False value
- False value
- False value
- False value
- False value
Conclusion
'as' and 'is' are used for casting. The 'is' keyword is one of the best approached to typecast but it has some limitations also. With 'as' keyword, we either get the converted value of another type or null while with 'is' keyword, we either get true or false. On the basis of the value, we execute the statements. I hope this article helps you to understand a bit more about 'is' and 'as' keywords.
Thank you and feel free to ask any question or make a suggestion.