diff --git a/docs/csharp/misc/cs0077.md b/docs/csharp/misc/cs0077.md index f44889dc71fa3..e8516b36d8c40 100644 --- a/docs/csharp/misc/cs0077.md +++ b/docs/csharp/misc/cs0077.md @@ -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 the [is](../language-reference/operators/is.md) operator, we can directly perform type checking and assignments in one step. The following sample generates CS0077: @@ -39,10 +41,13 @@ class M o1 = new C(); o2 = new S(); - s = o2 as S; - // CS0077, S is not a reference type. - // Try the following line instead. - // c = o1 as C; + s = o2 as S; // CS0077, S is not a reference type + + // Use pattern matching instead of as + if (o2 is S sValue) + { + s = sValue; + } } } ```