From 56907bba320c62ac96effcc2874d0c264d6949df Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 17 Oct 2024 08:14:21 -0700 Subject: [PATCH 1/6] Resolved issue 42861. --- docs/csharp/misc/cs0077.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/csharp/misc/cs0077.md b/docs/csharp/misc/cs0077.md index f44889dc71fa3..7b1f187d29369 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](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. The following sample generates CS0077: @@ -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; From 42eab321c89c597c4e217776b36c1e8a8de065ba Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 17 Oct 2024 10:40:35 -0700 Subject: [PATCH 2/6] Update docs/csharp/misc/cs0077.md Thank you for the suggestion. Updating it. Co-authored-by: Bartosz Klonowski <70535775+BartoszKlonowski@users.noreply.github.com> --- docs/csharp/misc/cs0077.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/misc/cs0077.md b/docs/csharp/misc/cs0077.md index 7b1f187d29369..b4835cf4a847a 100644 --- a/docs/csharp/misc/cs0077.md +++ b/docs/csharp/misc/cs0077.md @@ -14,7 +14,7 @@ The as operator must be used with a reference type or nullable type ('int' is a 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](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. +However, using [pattern matching](../fundamentals/functional/pattern-matching) with [is](../language-reference/operators/is) operator, we can directly perform type checking and assignments in one step. The following sample generates CS0077: From d941cf0a98db66e6fc609890a8baba56bee39a1b Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 17 Oct 2024 10:58:55 -0700 Subject: [PATCH 3/6] Update docs/csharp/misc/cs0077.md Co-authored-by: Bartosz Klonowski <70535775+BartoszKlonowski@users.noreply.github.com> --- docs/csharp/misc/cs0077.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/misc/cs0077.md b/docs/csharp/misc/cs0077.md index b4835cf4a847a..25832c86137a0 100644 --- a/docs/csharp/misc/cs0077.md +++ b/docs/csharp/misc/cs0077.md @@ -14,7 +14,7 @@ The as operator must be used with a reference type or nullable type ('int' is a 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) with [is](../language-reference/operators/is) operator, we can directly perform type checking and assignments in one step. +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: From 583e706edcf68acef2c14ea2e4e796a2b211f713 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 17 Oct 2024 11:25:10 -0700 Subject: [PATCH 4/6] Update docs/csharp/misc/cs0077.md Co-authored-by: Bartosz Klonowski <70535775+BartoszKlonowski@users.noreply.github.com> --- docs/csharp/misc/cs0077.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/csharp/misc/cs0077.md b/docs/csharp/misc/cs0077.md index 25832c86137a0..d81b31e6537c0 100644 --- a/docs/csharp/misc/cs0077.md +++ b/docs/csharp/misc/cs0077.md @@ -41,6 +41,8 @@ class M o1 = new C(); o2 = new S(); + s = o2 as S; // CS0077, S is not a reference type + // Use pattern matching instead of as if (o2 is S sValue) { From fc9533350a9a014447b27716ceb480315b3117cc Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 17 Oct 2024 13:01:58 -0700 Subject: [PATCH 5/6] Update cs0077.md --- docs/csharp/misc/cs0077.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/docs/csharp/misc/cs0077.md b/docs/csharp/misc/cs0077.md index d81b31e6537c0..4c74d295180b6 100644 --- a/docs/csharp/misc/cs0077.md +++ b/docs/csharp/misc/cs0077.md @@ -48,9 +48,6 @@ class M { s = sValue; } - // CS0077, S is not a reference type. - // Try the following line instead. - // c = o1 as C; } } ``` From 56f09651d019d3731f620ed7e05a7f0e6f43c0c4 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Fri, 18 Oct 2024 09:31:14 -0400 Subject: [PATCH 6/6] Update docs/csharp/misc/cs0077.md --- docs/csharp/misc/cs0077.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/misc/cs0077.md b/docs/csharp/misc/cs0077.md index 4c74d295180b6..e8516b36d8c40 100644 --- a/docs/csharp/misc/cs0077.md +++ b/docs/csharp/misc/cs0077.md @@ -14,7 +14,7 @@ The as operator must be used with a reference type or nullable type ('int' is a 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. +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: