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.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...