The out parameter inside a if is considered unassigned

I have this piece of code:

//EDIT
DateTime finalDate;
Dictionary<string, string> a = new Dictionary<string, string>() { { "a", "2016-04-14T11:56:56.0319859+02:00" } };
string dateStr;
DateTime date;
if (json.TryGetValue("a", out dateStr) && DateTime.TryParse(dateStr, out date))
{
   finalDate = date;
}

But I have an error inside if: "Using the unassigned local variable myDate". If I am not mistaken if this part is fulfilled, myDate should be assigned to TryParse (technically, even if the condition was false, it should also be assigned) if I put TryParse inside if it works. Is there a way to tell the compiler that it is ok?

+4
source share
2 answers

Solved by this comment:

I would guess that the important part you haven't mentioned is that json is declared as dynamic. This is a known issue in such a case.

From Damien_The_Unbeliever

+1
source

Installation is not guaranteed - ifShort-curcited statements if the first condition is not met.

You need to set myDateto

 DateTime myDate = DateTime.MinValue;
0

All Articles