Skip to content

Restore nullable attributes added in 9 #1221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: draft-v9
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
43 changes: 43 additions & 0 deletions standard/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,8 @@
`MaybeNullWhen` ([§22.5.7.7](attributes.md#22577-the-maybenullwhen-attribute)) | A non-nullable argument may be null when the method returns the specified `bool` value.
`NotNullWhen` ([§22.5.7.10](attributes.md#225710-the-notnullwhen-attribute)) | A nullable argument won’t be null when the method returns the specified `bool` value.
`NotNullIfNotNull` ([§22.5.7.9](attributes.md#22579-the-notnullifnotnull-attribute)) | A return value isn’t null if the argument for the specified parameter isn’t null.
`MemberNotNull` (§membernotnull-attribute) | The listed member won’t be null when the method returns.
`MemberNotNullWhen` (§membernotnullwhen-attribute) | The listed member won’t be null when the method returns the specified `bool` value.
`DoesNotReturn` ([§22.5.7.4](attributes.md#22574-the-doesnotreturn-attribute)) | This method never returns.
`DoesNotReturnIf` ([§22.5.7.5](attributes.md#22575-the-doesnotreturnif-attribute)) | This method never returns if the associated `bool` parameter has the specified value.

Expand Down Expand Up @@ -981,11 +983,11 @@
> #nullable enable
> public class X
> {
> private void ThrowIfNull([DoesNotReturnIf(true)] bool isNull, string argumentName)

Check warning on line 986 in standard/attributes.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/attributes.md#L986

MDC032::Line length 86 > maximum 81
> {
> if (!isNull)
> {
> throw new ArgumentException(argumentName, $"argument {argumentName} can't be null");

Check warning on line 990 in standard/attributes.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/attributes.md#L990

MDC032::Line length 96 > maximum 81
> }
> }
>
Expand Down Expand Up @@ -1030,6 +1032,47 @@

Specifies that a non-nullable argument may be `null` when the method returns the specified `bool` value. This is similar to the `MaybeNull` attribute ([§22.5.7.6](attributes.md#22576-the-maybenull-attribute)), but includes a parameter for the specified return value.

#### §membernotnull-attribute The MemberNotNull attribute

Specifies that the given member won’t be `null` when the method returns.

> *Example*: A helper method may include the `MemberNotNull` attribute to list any fields that are assigned to a non-null value in that method. A compiler that analyzes constructors to determine whether all non-nullable reference fields have been initialized may then use this attribute to discover which fields have been set by those helper methods. Consider the following example:
>
> <!-- Example: {template:"standalone-lib", name:"MemberNotNullAttribute"} -->
> ```csharp
> #nullable enable
> public class Container
> {
> private string _uniqueIdentifier; // must be initialized.
> private string? _optionalMessage;
>
> public Container()
> {
> Helper();
> }
>
> public Container(string message)
> {
> Helper();
> _optionalMessage = message;
> }
>
> [MemberNotNull(nameof(_uniqueIdentifier))]
> private void Helper()
> {
> _uniqueIdentifier = DateTime.Now.Ticks.ToString();
> }
> }
> ```
>
> Multiple field names may be given as arguments to the attribute’s constructor. *end example*

#### §membernotnullwhen-attribute The MemberNotNullWhen attribute

Specifies that the listed member won’t be `null` when the method returns the specified `bool` value.

> *Example*: This attribute is like `MemberNotNull` (§membernotnull-attribute) except that `MemberNotNullWhen` takes a `bool` argument. `MemberNotNullWhen` is intended for use in situations in which a helper method returns a `bool` indicating whether it initialized fields. *end example*

#### 22.5.7.8 The NotNull attribute

Specifies that a nullable value will never be `null` if the method returns (rather than throwing).
Expand All @@ -1039,7 +1082,7 @@
> <!-- Example: {template:"code-in-class-lib", name:"NotNullAttribute"} -->
> ```csharp
> #nullable enable
> public static void ThrowWhenNull([NotNull] object? value, string valueExpression = "") =>

Check warning on line 1085 in standard/attributes.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/attributes.md#L1085

MDC032::Line length 89 > maximum 81
> _ = value ?? throw new ArgumentNullException(valueExpression);
>
> public static void LogMessage(string? message)
Expand Down
16 changes: 16 additions & 0 deletions standard/standard-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
public class NullReferenceException : Exception
{
public NullReferenceException();
public NullReferenceException(string message);

Check warning on line 180 in standard/standard-library.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/standard-library.md#L180

MDC032::Line length 82 > maximum 81
public NullReferenceException(string message, Exception innerException);
}

Expand Down Expand Up @@ -225,7 +225,7 @@
public sealed class StackOverflowException : Exception
{
public StackOverflowException();
public StackOverflowException(string message);

Check warning on line 228 in standard/standard-library.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/standard-library.md#L228

MDC032::Line length 82 > maximum 81
public StackOverflowException(string message, Exception innerException);
}

Expand Down Expand Up @@ -387,7 +387,7 @@
public readonly ref struct ReadOnlySpan<T>
{
public int Length { get; }
public ref readonly T this[int index] { get; }

Check warning on line 390 in standard/standard-library.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/standard-library.md#L390

MDC032::Line length 86 > maximum 81
}

public readonly ref struct Span<T>
Expand Down Expand Up @@ -553,7 +553,7 @@

public readonly struct TaskAwaiter<TResult> : ICriticalNotifyCompletion,
INotifyCompletion
{

Check warning on line 556 in standard/standard-library.md

View workflow job for this annotation

GitHub Actions / Markdown to Word Converter

standard/standard-library.md#L556

MDC032::Line length 87 > maximum 81
public bool IsCompleted { get; }
public TResult GetResult();
}
Expand Down Expand Up @@ -614,6 +614,22 @@
public MaybeNullWhenAttribute(bool returnValue) {}
}

[System.AttributeUsage(System.AttributeTargets.Method |
System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)]
public sealed class MemberNotNullAttribute : Attribute
{
public MemberNotNullAttribute(string member) {}
public MemberNotNullAttribute(params string[] members) {}
}

[System.AttributeUsage(System.AttributeTargets.Method |
System.AttributeTargets.Property, AllowMultiple=true, Inherited=false)]
public sealed class MemberNotNullWhenAttribute : Attribute
{
public MemberNotNullWhenAttribute(bool returnValue, string member) {}
public MemberNotNullWhenAttribute(bool returnValue, params string[] members) {}
}

[System.AttributeUsage(System.AttributeTargets.Field |
System.AttributeTargets.Parameter | System.AttributeTargets.Property |
System.AttributeTargets.ReturnValue, Inherited=false)]
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy