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.
  1. using System;
  2. namespace Tutpoint
  3. {
  4. class Program
  5. {
  6. class Tutpoint
  7. {
  8. public Tutpoint()
  9. {
  10. Console.WriteLine("Hello TutPoint");
  11. }
  12. }
  13. static void Main(string[] args)
  14. {
  15. // Creating object of object type
  16. object obj = new object();
  17. Console.WriteLine(obj);
  18. // This will raise a runtime InvalidCastException as "Unable to cast object of type 'System.Object' to type 'Tutpoint'"
  19. Tutpoint tutpoint = (Tutpoint)obj;
  20. if (tutpoint != null)
  21. {
  22. Console.WriteLine("tutpoint does not contain null value");
  23. }
  24. else
  25. {
  26. Console.WriteLine("tutpoint contains null value");
  27. }
  28. Console.ReadKey();
  29. }
  30. }
  31. }
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?
  1. 'as' is a keyword used for conversion from one type to another. The type can be a reference or nullable.
  2. '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.
  3. 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
    1. Expression as dataType;
  4. We cannot perform conversion of value types (int, double, char, bool) and user-defined types.
  5. 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.
  6. 'as' improves the performance and it is safe for casting.
The above program can be rewritten using 'as' keyword.
  1. using System;
  2. namespace Tutpoint
  3. {
  4. class Program
  5. {
  6. class Tutpoint
  7. {
  8. public Tutpoint()
  9. {
  10. Console.WriteLine("Hello TutPoint");
  11. }
  12. }
  13. static void Main(string[] args)
  14. {
  15. // Creating object of object type
  16. object obj = new object();
  17. Console.WriteLine(obj);
  18. // 'as' keyword used to convert 'obj' of type 'object' to 'Tutpoint' type
  19. // It will return null as the conversion of object type to Tutpoint is incompatible
  20. Tutpoint tutpoint = obj as Tutpoint;
  21. if (tutpoint != null)
  22. {
  23. Console.WriteLine("tutpoint does not contain null value");
  24. }
  25. else
  26. {
  27. Console.WriteLine("tutpoint contains null value");
  28. }
  29. Console.ReadKey();
  30. }
  31. }
  32. }
Output
  1. System.Object
  2. tutpoint contains null value
Another example.
  1. using System;
  2. namespace Tutpoint
  3. {
  4. class Program
  5. {
  6. class Tutpoint
  7. {
  8. public string Name { get; set; }
  9. }
  10. class Tutlover
  11. {
  12. public string Name { get; set; }
  13. }
  14. static void Main(string[] args)
  15. {
  16. //Creating object of classes 'Tutpoint' and 'Tutlover'
  17. Tutpoint tutpoint = new Tutpoint();
  18. Tutlover tutlover = new Tutlover();
  19. //Creating array of objects
  20. object[] object_array = new object[7];
  21. object_array[0] = tutpoint;
  22. object_array[1] = tutlover;
  23. object_array[2] = "hey..Folks";
  24. object_array[3] = 1000;
  25. object_array[4] = 1.212;
  26. object_array[5] = 'c';
  27. object_array[6] = true;
  28. for (int i = 0; i < 7; i++)
  29. {
  30. // as-casting of obj_array[i] value to string. If compatible then return the value else return null
  31. string str = object_array[i] as string;
  32. if (str != null)
  33. {
  34. Console.WriteLine(str);
  35. }
  36. else
  37. {
  38. Console.WriteLine("Null value");
  39. }
  40. }
  41. Console.WriteLine();
  42. for (int i = 0; i < 6; i++)
  43. {
  44. // as-casting of obj_array[i] value to Tutlover type. If compatible then return the value else return null
  45. Tutlover obj = object_array[i] as Tutlover;
  46. if (obj != null)
  47. {
  48. Console.WriteLine(obj);
  49. }
  50. else
  51. {
  52. Console.WriteLine("Null value");
  53. }
  54. }
  55. Console.WriteLine();
  56. for (int i = 0; i < 6; i++)
  57. {
  58. // as-casting of obj_array[i] value to object. If compatible then return the value else return null
  59. object obj = object_array[i] as object;
  60. if (obj != null)
  61. {
  62. Console.WriteLine(obj);
  63. }
  64. else
  65. {
  66. Console.WriteLine("Null value");
  67. }
  68. }
  69. Console.ReadKey();
  70. }
  71. }
  72. }
Output:
  1. Null value
  2. Null value
  3. hey..Folks
  4. Null value
  5. Null value
  6. Null value
  7. Null value
  8. Null value
  9. Tutpoint.Program+Tutlover
  10. Null value
  11. Null value
  12. Null value
  13. Null value
  14. Tutpoint.Program+Tutpoint
  15. Tutpoint.Program+Tutlover
  16. hey..Folks
  17. 1000
  18. 1.212
  19. 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,
  1. 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,
  1. 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,
  1. 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??
  1. int int_value = object_array[i] as int;
  2. char char_value = object_array[i] as char;
  3. double double_value = object_array[i] as double;
  4. 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,
  1. 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.
  1. using System;
  2. namespace Tutpoint
  3. {
  4. class Program
  5. {
  6. class Tutpoint
  7. {
  8. public string Name { get; set; }
  9. }
  10. class Tutlover
  11. {
  12. public string Name { get; set; }
  13. }
  14. static void Main(string[] args)
  15. { //Creating object of classes 'Tutpoint' and 'Tutlover'
  16. Tutpoint tutpoint = new Tutpoint();
  17. Tutlover tutlover = new Tutlover();
  18. //Creating array of objects
  19. object[] object_array = new object[7];
  20. object_array[0] = tutpoint;
  21. object_array[1] = tutlover;
  22. object_array[2] = "hey..Folks";
  23. object_array[3] = 1000;
  24. object_array[4] = 1.212;
  25. object_array[5] = 'c';
  26. object_array[6] = true;
  27. for (int i = 0; i < 7; i++)
  28. {
  29. // 'is' will check whether obj_array[i] value is compatible with string type. If compatible then return true else return false
  30. Boolean result = object_array[i] is string;
  31. if (result)
  32. {
  33. Console.WriteLine("True value");
  34. // Write the statements which are to be executed when result will be true
  35. }
  36. else
  37. {
  38. Console.WriteLine("False value");
  39. // Write the statements which are to be executed when result will be true
  40. }
  41. }
  42. Console.WriteLine();
  43. for (int i = 0; i < 7; i++)
  44. {
  45. // 'is' will check whether obj_array[i] value is compatible with object type. If compatible then return true else return false
  46. Boolean result = object_array[i] is object;
  47. if (result)
  48. {
  49. Console.WriteLine("True value");
  50. // Write the statements which are to be executed when result will be true
  51. }
  52. else
  53. {
  54. Console.WriteLine("False value");
  55. // Write the statements which are to be executed when result will be true
  56. }
  57. }
  58. Console.WriteLine();
  59. for (int i = 0; i < 7; i++)
  60. {
  61. // 'is' will check whether obj_array[i] value is compatible with Tutlover type. If compatible then return true else return false
  62. Boolean result = object_array[i] is Tutlover;
  63. if (result)
  64. {
  65. Console.WriteLine("True value");
  66. // Write the statements which are to be executed when result will be true
  67. }
  68. else
  69. {
  70. Console.WriteLine("False value");
  71. // Write the statements which are to be executed when result will be true
  72. }
  73. }
  74. Console.WriteLine();
  75. Console.ReadKey();
  76. }
  77. }
  78. }
Output
  1. False value
  2. False value
  3. True value
  4. False value
  5. False value
  6. False value
  7. False value
  8. True value
  9. True value
  10. True value
  11. True value
  12. True value
  13. True value
  14. True value
  15. False value
  16. True value
  17. False value
  18. False value
  19. False value
  20. False value
  21. 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.