我如何将Switch
语句与TimeSpan
?
switch (myTimeSpan)
{
case TimeSpan.Minutes:
Console.WriteLine("Case 1");
break;
case TimeSpan.Hours:
Console.WriteLine("Case 2");
break;
case TimeSpan.Seconds:
Console.WriteLine("Case 2");
break;
}
编辑 - 添加答案
C#7 可以处理Switch
带有模式匹配的语句
switch(DateTime.Now-lastDate) {
case var t when t.Days>720:
changefreq="never";
break;
case var t when t.Days>365:
changefreq="yearly";
break;
case var t when t.Days>180:
changefreq="monthly";
break;
case var t when t.Days>30:
changefreq="weekly";
break;
default:
changefreq="daily";
break;
}