Discussing the "is" and "as" Operators of C#

Today, we will discuss the "is" and "as" operators in the C# language.
The "as" and "is" keywords are useful for avoiding type mismatch exceptions or other type exceptions when doing type casting.
Also, we can use these operators to check object compatibility.

C# "is" operator

In the C# language, we use the "is" operator to check the object type. If two objects are of the same type, it returns true. Else it returns false. For example, if you need to check if an object is a String type or any other Type type.
Let's try to understand this using C# example. 
We declare two classes, Speaker and Author.
  1. class Speaker  
  2.     { public string Name { getset; } }  
  1. class Author  
  2.     { public string Name { getset; } }  
The following code creates an Speaker type ojbect, var speaker.
  1. var speaker = new Speaker { Name="Gaurav Kumar Arora"};  
Now, somewhere in code, we need to check if the "speaker" var is of type Speaker, then do certain operations. The following code checks if speaker is of type Speaker. If yes, the return is true. If not, the result is false. 
  1. var isTrue = speaker is Speaker;  
In the preceding, we are just checking the matching type. Yes, our speaker is an object of Speaker type.
  1. Console.WriteLine("speaker is of Speaker type:{0}", isTrue);  
So, the results is true.
Now, let's try same and create another var of type Author. The following code creates an object of Author and compares if speaker is of the Author type. Obviously its not. The result will be false. 
  1. var author = new Author { Name = "Gaurav Kumar Arora" };  
  2. var isTrue = speaker is Author;  
  3. Console.WriteLine("speaker is of Author type:{0}", isTrue);  
Why do we need "is" operator
When you work in large software, certain operations are allowed on certain types including type casting. For example, if you convert a string type to an int where the value of the string is not a number, there will be an exception. 
The "is" operator is useful to check the type before you perfom the conversion.

C# "as" operator

The "as" operator behaves similar to the "is" operator. The only difference is, it returns the object if both types are compatible. Else it returns a null.
In the below code sample, object is a dynamic variable and it is being compared as Author type. If you do not pass an Author type, the method will return a null.
  1. public static string GetAuthorName(dynamic obj)  
  2. {  
  3. Author authorObj = obj as Author;  
  4. return (authorObj != null) ? authorObj.Name : string.Empty;  
  5. }  
Here, we declared two variables of type Speaker and Author:
  1. var speaker = new Speaker { Name="Gaurav Kumar Arora"};  
  2. var author = new Author { Name = "Gaurav Kumar Arora" };  
The following returns the "Name" property:
  1. var authorName = GetAuthorName(author);  
  3. Console.WriteLine("Author name is:{0}", authorName);  
The following returns an empty string:
  1. authorName = GetAuthorName(speaker);  
  3. Console.WriteLine("Author name is:{0}", authorName);  
In this article, you learned how to use "as" and "is" operators of the C# language.

Similar Articles