Skip to content

"IDE0090: 'new' expression can be simplified" changes the behavior for Nullable<T> #54081

@ajtribick

Description

@ajtribick

Version Used:

Compiler version: '3.10.0-4.21269.26 (0298477). Language version 9.0.

Steps to Reproduce:

int? x = new int?(); // sets x to null
int? y = new();      // sets y to 0
System.Console.WriteLine($"x = {x}");
System.Console.WriteLine($"y = {y}");

Expected Behavior:

In my opinion, the least surprising option would be to have the second line should behave the same way as the first, i.e. y should be initialized to null in the above program, although the C# 9 spec implies that the current behavior is correct.

If these cases are intended to produce different results, then the IDE0090 suggestion should not be shown.

Actual Behavior:

The first line (setting x) receives a suggestion "IDE0090: 'new' expression can be simplified" - applying this suggestion changes the behavior of the program as x ends up being initialized to 0.

Output:

x = 
y = 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions