Compiler Error CS0077
The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type).
The as operator was passed a value type. Because as
can return null, it can only be passed a reference type or a nullable value type.
However, using pattern matching with the is operator, we can directly perform type checking and assignments in one step.
The following sample generates CS0077:
// CS0077.cs
using System;
struct S
{
}
class M
{
public static void Main()
{
object o;
S s;
o = new S();
s = o as S; // CS0077, S is not a reference type
// Use pattern matching instead of as
if (o is S sValue)
{
s = sValue;
}
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET feedback
.NET is an open source project. Select a link to provide feedback: