rksoftware

Visual Studio とか C# とかが好きです

明確な代入分析の改善

■ C# 10.0 での新機能

・明確な代入分析の改善 (Improved definite assignment analysis)
  https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/improved-definite-assignment
 変数に値がセットされているかの分析が機能するパターンが増えた。

#nullable enable

C c = new C();
if (c?.M(out object obj3) == true)
{
    obj3.ToString(); // undesired error
}

if (c?.M(out object obj4) ?? false)
{
    obj4.ToString(); // undesired error
}
if (c != null ? c.M(out object obj) : false)
{
    obj.ToString(); // undesired error
}

public class C
{
    public bool M(out object obj)
    {
        obj = new object();
        return true;
    }
}