-
Hi, How can I tell the compiler that the // TryParse below use a custom Interpolated String Handler
if ("Name: FaustVX Age: 32".TryParse($"Name: {Get(out string? name)} Age: {Get(out int age)}"))
Console.WriteLine($"{name}, {age}"); // <-- error CS0165: Use of unassigned local variable 'name' and 'age' Here is a shortened version on sharplab.io |
Beta Was this translation helpful? Give feedback.
Answered by
333fred
Jul 15, 2023
Replies: 1 comment 1 reply
-
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
FaustVX
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem with both the gist and this simpler example is that the compiler has no way to relate the
bool
returns of the handler to the result ofTryParse
. For all it knows, one of theAppendX
methods could have failed, causing the subsequent ones to never be run. In order to get this (and the gist) to work, you'd have to make them returnvoid
, notbool
, and have the handler just no-op for subsequent calls: https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQAwAIpwHQCUCuAdgC4CWAtgKbYDCA9uQA6kA2lATgMocBupAxpQDOAbmRpMcAGzoACgEN2Q0oQDmAUQAexSoWV09YpMlIAzdAAoARADl5VEOgBi8/EOIA1ABroAgqspHAGYYK2wAFXYATwUlSgsAElt7QPQAbwBxSmILOnxiSVQAfnRCFIBKAF8/AMdM7Nz89BUC+QCqq3Ly5DTkdH7JAE5rMqpOowH0AHoprGGktNHKSvHpqfQ…