![]() Is not guaranteed to initialize the local variable value. I think the error is valid due to the fact that this line if (myDict?.TryGetValue("hello", out var value) = true) In this fiddle,, you can see the error, which is omitted in the mono compiler. ![]() An easier solution is to give something like "unable to determine assignment status" as warning instead of error, if compiler can't look forward for assignment status when interpreting null conditional operator. Whenever CPU executing to the point inside the if bracket code block, the value must have returned from TryGetValue call and is NOT "unassigned local variable". I think it's an true issue/bug if the requirement for compiler includes not to give false alarm. The called method is required to assign a value before the method returns. ![]() If (myDict?.TryGetValue("hello", out var value) = true)Ĭonsole.WriteLine("Hello" + value.ToString()) Įxpecting to see Hello1 in Console.Outputīecause if the condition is true, the Null-Conditional check must have returned a non-null value, and the key is existing in the dictionary and the value must have been assigned when returning from the TryGetValue method. Microsoft (R) Visual C# Compiler version 2.10.0.0 (b9fb1610) The code can compile and run online, however, it doesn't compile in my local visual studio. I put the code below, and also uploaded to a online c# compiler:
0 Comments
Leave a Reply. |