declaring var inside if statement c#

前端 未结 3 1069
遇见更好的自我
遇见更好的自我 2021-01-24 19:11

I have this var, but I want to change it\'s content depending on the statement, I can\'t get it working because when I use it, VS says it has not been declared, even if the stat

3条回答
  •  执笔经年
    2021-01-24 19:35

    Declare the variable outside the scope of the if statement:

    IEnumerable days;
    
    if (DateTime.Today.Day > 28 && DateTime.Today.Day < 2)
    {
        days = GetDaysLikeMe(calendario.Value.Date).Take(50).Where(d => d.Date.Day > 28 && d.Date.Day < 2).Take(4);
    }
    else
    {
        days = GetDaysLikeMe(calendario.Value.Date).Take(50).Where(d => d.Date.Day < 28 && d.Date.Day > 2).Take(4);
    }
    

提交回复
热议问题