Skip to content

Null-safe navigation and null-coalescing break the out variable initialization detection #33559

@kodfodrasz

Description

@kodfodrasz

Version Used:

C# 7.3

Excerpt from Visual Studio About dialog (items suspected to be relevant)

Microsoft Visual Studio Enterprise 2017 
Version 15.9.2
VisualStudio.15.Release/15.9.2+28307.108
Microsoft .NET Framework
Version 4.7.03056
C# Tools   2.10.0-beta2-63501-03+b9fb1610c87cccc8ceb74a770dba261a58e39c4a
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Steps to Reproduce:

  1. given the following example:
                var place = new
                {
                     Metadata = (IDictionary<string, string>)null
                };
                // ...
                var sb = new StringBuilder();
                // ...
                if(place.Metadata?.TryGetValue("erpid", out var erpid) ?? false)
                {
                    sb.Append(" | ");
                    sb.Append("erpid=");
                    sb.Append(erpid);
                }

where Metadata is IDictionary<string,string> typed nullable member of place.

  1. I get Error CS0165 Use of unassigned local variable 'erpid'
    image
  2. code does not compile

Expected Behavior:

erpid is initialized in the scope, as the expression can be evaluated at compile time to determine that the erpid variable will be initialized in the scope.

Example code compiles correctly.

Actual Behavior:

Null-safe navigation and null-coalescing break the out variable initialization detection.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions