开发者

Explain the DateTime.Now design in .Net

开发者 https://www.devze.com 2023-04-08 09:10 出处:网络
I have a question regarding the design of the Date object in the .Net framework, more specifically the Now property.

I have a question regarding the design of the Date object in the .Net framework, more specifically the Now property.

I know that Now is a property that is a Date type. (This makes sense since I see the methods .AddHours(), AddYears(), etc) If Now being a class(or struct) how does it return a value directly.

For example: How would I create a class or struct in C#/VB.net that would allow me to do the same without calling a property of Now?

(VB.Net) Dim WeatherObject as new WeatherReporter()

If WeeatherReport had a property called TodaysWeather that was a class how could I then get a value like this

Dim PlainTextReport as string=WeeatherReport.TodaysWeather

To me it seems like you would have to call a another property off of TodaysWeather. But with the Datetime.Now example you don't. Anyways, I know its an odd question. I was just trying to better understand how some of these objects are set up under the hood

Thanks for any light on the matter.

Here is an example of code referring to the above question.

Public Class WeatherReporter
    Shared ReadOnly Property TodaysWeather As DayWeather
        Get
            TodaysWeather = New DayWeather
            Return TodaysWeather.Current
        End Get
    End Property
End Class

Public Class DayWeather
    Public Property Current As String = "Sunny"
End Class

Now similar to the DateTime.Now object

How could one you the weather example like this

Dim TheForecast as string=WeatherRepor开发者_如何转开发ter.TodaysWeather 

It seems like it would require the following

Dim TheForecast as string=WeatherReporter.TodaysWeather.Current

I know its a confusing question, lol. thanks for your patience


Reflecting the source code it's evident that it's simply a property wrapper around a function.

public static DateTime Now
{
    get
    {
        return UtcNow.ToLocalTime();
    }
}

You don't need to instantiate a DateTime object since the property is marked as static.


.NET properties are more than merely values - they encapsulate getter and setter methods around underlying values. That being the case, they can initialize objects before returning them. So, for example, DateTime.Now may be implemented like this (without attempting to find its actual implementation via Reflector or Reference Sources...):

public static DateTime Now
{
    get
    {
        var d = new DateTime() { .Kind = DateTimeKind.Local };
        // Logic to determine the current system time and set d to that value
        return d;
    }
}

(ref: MSDN)


DateTime.Now looks something like this in VB.net...

public structure DateTime

    ...other code...

    ''// `shared` makes this a class property, not an instance property.
    ''// This is what lets you say `DateTime.Now`
    public shared readonly property Now as DateTime
        get
            ... do some magic to return the current date/time ...
        end get
    end property

    ... other code ...

end class

or in C#:

public struct DateTime
{
    ... other code ...

    // `static` works like VB's `shared` here
    public static DateTime Now
    {
        get { /* do your magic to return the current date/time */ }
    }

    ... other code ...
}


It's implemented as a static getter method. I believe what your looking for would look something like this in your WeatherReporter class:

Public Shared ReadOnly Property TodaysWeather As WeatherReporter
    Get
        Return New WeatherReporter(DateTime.Now)
    End Get
End Property

That assumes that your WeatherReporter class has a constructor that accepts a DateTime.


DateTime.Now is a static/Shared property. You don't have to instantiate a new object (using New DateTime()) to call it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号