DateTime.Today and "static readonly"
DateTime.Today
is static readonly
. So supposedly it should never change once (statically) instantiated.
However -- if I fire up an applica开发者_如何学Gotion and ask for the date at 11:59pm and then again at 12:01am, it will correctly give me different values each time i call it, right?
Let's say I wanted to create a static readonly
"DateTime.TwoDaysFromNow
" (just a representative example) that behaves the same way. .NET will tell me I can't b/c it's a readonly
remember! How can I make it work?
Much appreciated, -Alan.
It's a static readonly property, not a static readonly field:
public static DateTime Today
{
get
{
return Now.Date;
}
}
public static DateTime TwoDaysFromNow
{
get { return DateTime.Today.AddDays(2); }
}
You can tell DateTime.Today
is a property from Microsoft's Syntax of it:
public static DateTime Today { get; }
精彩评论