本文介绍了在if语句中声明var c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个 var,但我想根据语句更改它的内容,我无法让它工作,因为当我使用它时,VS 说它尚未声明,即使该声明为真...
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 statement is true...
if (DateTime.Today.Day > 28 && DateTime.Today.Day < 2)
{
var days = GetDaysLikeMe(DateTime.Today).Take(50).Where(d => d.Date.Day > 28 && d.Date.Day < 2).Take(4);
}
else
{
var days = GetDaysLikeMe(DateTime.Today).Take(50).Where(d => d.Date.Day < 28 && d.Date.Day > 2).Take(4);
}
我试图在框外声明变量......但也不能让它工作,我在 var 日保持的函数是这个
I've tried to declare the variable outside the box... But can't get it working neither, the function I keep on var days is this
public IEnumerable<DateTime> GetDaysLikeMe(DateTime currentDate)
{
DateTime temp = currentDate;
while (true)
{
temp = temp.AddDays(-7);
yield return temp;
}
}
推荐答案
声明 if 语句范围之外的变量:
Declare the variable outside the scope of the if statement:
IEnumerable<DateTime> 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);
}
这篇关于在if语句中声明var c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!