作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下 AddTicks
方法的代码。 datetime 对象的 Ticks 属性在 AddTick 方法前后返回相同的值。为什么会这样?
There are 10,000 ticks in a millisecond.
Ticks: The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue.
AddTicks : Adds the specified number of ticks to the value of this instance.
注意:我使用的是.Net 4.0
框架
代码
static void Main()
{
DateTime dt2 = new DateTime(2010, 5, 7, 10, 11, 12, 222);
long x = dt2.Ticks;
dt2.AddTicks(9999);
long y = dt2.Ticks;
bool isSame = false;
if (x == y)
{
isSame = true;
}
Console.WriteLine(isSame);
System.Console.ReadKey();
}
最佳答案
AddTicks
(以及其他 Add*
方法)不会更改 DateTime,但会返回一个新对象。
所以你应该使用
dt2 = dt2.AddTicks(...)
DateTime
是一个值类型并且是不可变的。
关于c# - 使用 AddTicks 不会对 DateTime 造成影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14253824/
我有以下 AddTicks 方法的代码。 datetime 对象的 Ticks 属性在 AddTick 方法前后返回相同的值。为什么会这样? There are 10,000 ticks in a m
我是一名优秀的程序员,十分优秀!