[Solved] Use of unassigned local variable when trying to return bool


The error you’re getting is that you’re attempting to use dateCheck in it’s initial assignment, which is not allowed. In otherwords, you can’t do something like int number = number + 1; because number on the right hand side of the assignment is not assigned yet. Of course the line in question must have some other typos in it, because it won’t compile at all.

Regardless, you don’t really need that line anyway (the variable isn’t used anywhere else). If you remove it, your code should work as expected.

The one change I would make is to not hard-code the year portion but instead use the year specified in the date parameter. This way the method will work in future years. Even better would be to read the peak date ranges from some other data source, so they can be modified without having to recompile the code. But the basic idea I’m suggesting would look like this:

public bool IsInPeakSeason(DateTime date)
{
    var startPeak = new DateTime(date.Year, 06, 15);
    var endPeak = new DateTime(date.Year, 08, 15);
    return date >= startPeak && date < endPeak;
}

0

solved Use of unassigned local variable when trying to return bool