Today I encountered (by mistake) a weird compiler error and I don't understand the reason for it (compiler issue maybe?). .Net Framework 4.0 and Visual Studio 2019 if that matters.
The exact error is "Use of unassigned local variable 'value'" at the if after the TryParse
.
The code compiles fine if I use s
or I cast d.s
to string.
using System;
using System.Dynamic;
namespace TestConsoleApp
{
static class Program
{
static void Main(string[] _)
{
string s = "1";
dynamic d = new ExpandoObject();
d.s = s;
if (d.s != null && int.TryParse(d.s, out int value))
{
if (value == 1)
{
Console.Out.WriteLine("OK!");
}
}
}
}
}