You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't understand why I get an error if I use ?. but not if I use != null in the example below.
Version Used:
Visual Studio 16.6.3, .NET Core 3.1 Console Application
Steps to Reproduce:
using System.Collections.Generic;
namespace RoslynTest
{
class Program
{
class SomeClass
{
public Dictionary<string, string> SomeProperty { get; } = new Dictionary<string, string>();
}
static SomeClass? MyProperty { get; }
static void Main(string[] args)
{
{
// Doesn:t work
if (MyProperty?.SomeProperty.TryGetValue("key", out var value) == true)
{
//Error CS0165 Use of unassigned local variable 'value' RoslynTest C:\Users\h165\source\repos\RoslynTest\RoslynTest\Program.cs 21 Active
value.ToString();
}
// Works
if (MyProperty != null && MyProperty.SomeProperty.TryGetValue("key", out var value2))
{
//no error
value.ToString();
}
}
}
}
}
Expected Behavior:
No errors
Actual Behavior:
CS0165
The text was updated successfully, but these errors were encountered:
This is definite assignment analysis working as specified. Please see #45582 (comment) and consider searching for or filing an issue at https://github.com/dotnet/csharplang if you wish to propose a change to the specification.
I don't understand why I get an error if I use
?.
but not if I use!= null
in the example below.Version Used:
Visual Studio 16.6.3, .NET Core 3.1 Console Application
Steps to Reproduce:
Expected Behavior:
No errors
Actual Behavior:
CS0165
The text was updated successfully, but these errors were encountered: