C# | as Operator Keyword

In the development of the software, typecasting is an inescapable thing. In many cases, developer need to convert an Object(Type) into another Object(Type) and sometimes he/she may get InvalidCastException. So, to overcome such types of exception C# provides as operator keyword.

The as operator is used to perform conversion between compatible reference types or Nullable types. This operator returns the object when they are compatible with the given type and return null if the conversion is not possible instead of raising an exception. The working of as operator is quite similar to is an operator but in shortening manner.


expression as type

The above syntax is equivalent to below code. But the expression variable will be evaluated only one time.

expression is type ? (type)expression : (type)null

Here, ‘is‘ is an operator keyword.

Note: The ‘as’ operator keyword in C# is used only for nullable, reference and boxing conversions. It can’t perform user-defined conversions that can be only performed by using cast expression.

Example 1: In the below code, str1 contains a string which is assigned to a variable obj1 of the object type. Now, this obj1 is cast to string using as operator and assign the case result to variable str2 of string type. If the case successful, it returns the result otherwise returns null. Here, if(str2 != null) is used to check whether the result is null or not. For List<string> mylist = obj1 as List<string> cast fails and it returns null.

// C# program to illustrate 
// the use of 'as' operator
using System;
using System.Text;
using System.Collections.Generic;
class GFG {
    // Main Method
    public static void Main() {
        // taking a string variable
        string str1 = "GFG";
        // taking an Object type variable
        // assigning var1 to it
        object obj1 = str1;
        // now try it to cast to a string
        string str2 = obj1 as string;
        // checking Successfully cast or not
        if(str2 != null)
            Console.WriteLine("Successfully Cast");
        // now try to cast it to List
        List<string> mylist = obj1 as List<string>;
        // checking Successfully cast or not
        if(mylist != null)
            Console.WriteLine("Successfully Cast");
            Console.WriteLine("Not Successfull");


Successfully Cast
Not Successfull

Example 2: In the code, we are taking an Object array which can store five elements. The first and second elements are the instance of class Geeks1 and class Geeks2. The third element is a string and the fourth element is a double value and the fifth element is a null value. Here, string str = obj[j] as string; we are using as operator to cast the object array as a string and store result into the string str. After that, check for the resultant value. If it is null then print the “element is not a string” and if not null, then print the string.

// C# program to illustrate the
// concept of 'as' operator
using System;
// Classes
class Geeks1 { }
class Geeks2 { }
class GFG {
    // Main method
    static void Main()
        // creating and initializing object array
        object[] obj = new object[5];
        obj[0] = new Geeks1();
        obj[1] = new Geeks2();
        obj[2] = "C#";
        obj[3] = 334.5;
        obj[4] = null;
        for (int j = 0; j < obj.Length; ++j) {
            // using as operator
            string str = obj[j] as string;
            Console.Write("{0}:", j);
            // checking for the result
            if (str != null) {
                Console.WriteLine("'" + str + "'");
            else {
                Console.WriteLine("Is is not a string");


0:Is is not a string
1:Is is not a string
3:Is is not a string
4:Is is not a string

This article is attributed to GeeksforGeeks.org

leave a comment



load comments

Subscribe to Our Newsletter