C# Error CS0077 – The as operator must be used with a reference type or nullable type

C# Compiler Error

CS0077 – The as operator must be used with a reference type or nullable type (‘int’ is a non-nullable value type).

Reason for the Error

You will receive this error when you use as operator and pass a value type. For example, try compiling the below code.

public class DeveloperPublish
{
    public static void Main()
    {
        float j = 2.0f;
        var result = j as int;

    }
}

You will receive the below error since you are using the as operator and passing a int as type. The ‘as’ operator in C# can only be passed a reference type or nullable value types in C#.

Error CS0077 The as operator must be used with a reference type or nullable type (‘int’ is a non-nullable value type)

C# Error CS0077 – The as operator must be used with a reference type or nullable type

Solution

To fix the error CS0077 in C#, you will need to avoid passing the value type and use reference types when you are using the ‘as’ operator.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Hey, wait!Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.