Skip to content

Commit 56907bb

Browse files
author
Adit Sheth
committed
Resolved issue 42861.
1 parent cc8f93f commit 56907bb

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

docs/csharp/misc/cs0077.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ ms.assetid: 55d3d290-d172-41a3-b326-ebf5a0a7e81f
1313
The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type).
1414

1515
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).
16+
17+
However, using [pattern matching](https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/pattern-matching) with [is](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/is) operator, we can directly perform type checking and assignments in one step.
1618

1719
The following sample generates CS0077:
1820

@@ -39,7 +41,11 @@ class M
3941
o1 = new C();
4042
o2 = new S();
4143

42-
s = o2 as S;
44+
// Use pattern matching instead of as
45+
if (o2 is S sValue)
46+
{
47+
s = sValue;
48+
}
4349
// CS0077, S is not a reference type.
4450
// Try the following line instead.
4551
// c = o1 as C;

0 commit comments

Comments
 (0)