Skip to content

Commit 1477dfc

Browse files
authored
tweak MaybeNull attribute examples (#1456)
1 parent b637ac5 commit 1477dfc

File tree

1 file changed

+1
-10
lines changed

1 file changed

+1
-10
lines changed

standard/attributes.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -982,23 +982,14 @@ Specifies that a non-nullable return value may be null.
982982
983983
> *Example*: Consider the following generic method:
984984
>
985-
> <!-- TODO: This sample no longer generates the error in C# 9. This could be part of the new nullable constraints. Was: , expectedErrors:["CS8627"]-->
986-
> <!-- Example: {template:"code-in-class-lib", name:"MaybeNull1Attribute", replaceEllipsis:true, customEllipsisReplacements: ["return default;"]} -->
987-
> ```csharp
988-
> #nullable enable
989-
> public T? Find<T>(IEnumerable<T> sequence, Func<T, bool> predicate) { ... }
990-
> ```
991-
>
992-
> The idea of this code is that if `T` is replaced by `string`, `T?` becomes a nullable annotation. However, this code is not legal because `T` is not constrained to be a reference type. However, adding this attribute solves the problem:
993-
>
994985
> <!-- Example: {template:"code-in-class-lib", name:"MaybeNull2Attribute", replaceEllipsis:true, customEllipsisReplacements: ["return default;"]} -->
995986
> ```csharp
996987
> #nullable enable
997988
> [return: MaybeNull]
998989
> public T Find<T>(IEnumerable<T> sequence, Func<T, bool> predicate) { ... }
999990
> ```
1000991
>
1001-
> The attribute informs callers that the contract implies a non-nullable type, but the return value may actually be `null`. *end example*
992+
> Without the attribute the compiler might generate a warning if the method could return `null`. The presence of the attribute suppresses that warning. *end example*
1002993
1003994
#### 23.5.7.7 The MaybeNullWhen attribute
1004995

0 commit comments

Comments
 (0)