Discussing The "Is" And "As" Operators Of C#

Introduction

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.

class Speaker
{
    public string Name { get; set; }
}
class Author
{
    public string Name { get; set; }
}

The following code creates an Speaker type ojbect, var speaker.

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.

var isTrue = speaker is Speaker;

In the preceding, we are just checking the matching type. Yes, our speaker is an object of Speakertype.

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.

var author = new Author { Name = "Gaurav Kumar Arora" };
var isTrue = speaker is Author;
Console.WriteLine("speaker is of Author type: {0}", isTrue);

Why do we need 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, the 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.

public static string GetAuthorName(dynamic obj)
{
    Author authorObj = obj as Author;
    return (authorObj != null) ? authorObj.Name : string.Empty;
}

Here, we declared two variables of type Speaker and Author.

var speaker = new Speaker { Name = "Gaurav Kumar Arora" };
var author = new Author { Name = "Gaurav Kumar Arora" };

The following returns the "Name" property.

var authorName = GetAuthorName(author);
Console.WriteLine("Author name is: {0}", authorName);

The following returns an empty string.

authorName = GetAuthorName(speaker);
Console.WriteLine("Author name is: {0}", authorName);

Conclusion

In this article, you learned how to use "as" and "is" operators of the C# language.


Similar Articles