Skip to content

Conditional assignment does not set non-null initialization state #41273

@sharwell

Description

@sharwell

Version Used: SharpLab x64 (unknown commit)

Steps to Reproduce:

#nullable enable

using System;

public class C1 {
    string x;
    
    public C1()
    {
        try
        {
            x = "";
        }
        catch (Exception)
        {
            x = "";
        }
    }
}

public class C2 {
    string x;
    
    public C2() // warning CS8618: Non-nullable field 'x' is uninitialized. Consider declaring the field as nullable.
    {
        try
        {
            x = "";
        }
        catch (Exception)
        {
            x ??= "";
        }
    }
}

Expected Behavior:

No warning for either C1 or C2.

Actual Behavior:

A warning (CS8618) is reported for C2.

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