I have following code for AddTicks method. Ticks property of datetime object is returning same value before and after the AddTick method. Why is it behaving so?
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.
Note: I am using .Net 4.0 framework
CODE
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(and the otherAdd*methods) does not alter the DateTime, but returns a new object.So you should use
DateTimeis a value type and is immutable.