1.TimeSpan
- 时间 1 是 2011-1-2 1:00:01;
- 时间 2 是 2011-1-12 1:00:00。
用时间 2 减时间 1,得到一个 TimeSpan 实例。
那么时间 2 比时间 1 多 9 天 23 小时 59 分 59 秒。
那么,Days 就是 9,Hours 就是 23,Minutes 就是 59,Seconds 就是 59。
2.逻辑运算符
C#提供“与”和“或”逻辑运算符,他们能够产生更高效的代码。
在“与”运算符中(&&),如果第一个操作数为假,那么无需考虑第二个操作数,其结果都为假
在“或”运算符中(||),如果第一个操作数为真,那么无需考虑第二个操作数,其结果都为真
在这两种情况下,无需计算第二个操作数的值,产生高效代码
他们的常规样式“&”和“|”,常规样式总是计算每个操作数
注意:在某些情况下,会有副作用
//老梅
using System
class SideEffects
{
static void Main()
{
int i;
bool someCondition = false;
i = 0;
// Here, i is still incremented even though the if statement fails.
if (someCondition & (++i < 100))
Console.WriteLine("this won't be displayed");
Console.WriteLine("if statement executed: " + i); // displays 1
// In this case, i is not incremented because the short-circuit
// operator skips the increment.
if (someCondition && (++i < 100))
Console.WriteLine("this won't be displayed");
Console.WriteLine("if statement executed: " + i); // still 1 !!
}
}