Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/csharp/misc/cs0077.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ ms.assetid: 55d3d290-d172-41a3-b326-ebf5a0a7e81f
The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type).

The [as](../language-reference/operators/type-testing-and-cast.md#as-operator) operator was passed a [value type](../language-reference/builtin-types/value-types.md). Because `as` can return [null](../language-reference/keywords/null.md), it can only be passed a [reference type](../language-reference/keywords/reference-types.md) or a [nullable value type](../language-reference/builtin-types/nullable-value-types.md).

However, using [pattern matching](../fundamentals/functional/pattern-matching.md) with [is](../language-reference/operators/is.md) operator, we can directly perform type checking and assignments in one step.

The following sample generates CS0077:

Expand All @@ -39,7 +41,11 @@ class M
o1 = new C();
o2 = new S();

s = o2 as S;
// Use pattern matching instead of as
if (o2 is S sValue)
{
s = sValue;
}
// CS0077, S is not a reference type.
// Try the following line instead.
// c = o1 as C;
Expand Down
Loading