Skip to content

Warning reported for assignment to out parameter with [MaybeNull] or [MaybeNullWhen(...)] #39922

@cston

Description

@cston

No warning should be reported for the assignments:

#nullable enable

using System.Diagnostics.CodeAnalysis;

class Program
{
    static void GetValue<T>([AllowNull]T x, [MaybeNull]out T y)
    {
        y = x; // warning CS8601: Possible null reference assignment
    }

    static bool TryGetValue<T>([AllowNull]T x, [MaybeNullWhen(false)]out T y)
    {
        y = x; // warning CS8601: Possible null reference assignment
        return y != null;
    }
}

For [MaybeNullWhen(...)], perhaps the compiler should allow assignment without warning to the variable regardless of the return value.

cc @stephentoub

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions