开发者

Is there a class that holds both dateTime and Timespan together?

开发者 https://www.devze.com 2023-02-11 09:11 出处:网络
I\'m looking for a class that represents an concurrent event, for example: On every work 开发者_StackOverflow社区day, meeting from 10:00 that takes 2 hours. etc..

I'm looking for a class that represents an concurrent event, for example: On every work 开发者_StackOverflow社区day, meeting from 10:00 that takes 2 hours. etc..

I know how to implement it my self, i wondered if there is a predifined class.

tnx!


No, but here's one to take home:

public class DateTimeAndTimeSpan
{
    public DateTime Time{get;set;}
    public TimeSpan Duration{get;set;}
}


I'd make such a class immutable, and perhaps even a struct.

You can choose either a start/end or start/duration internal representation. Should be invisible to the public API.

If you're fancy you can overload a few operators and override Equals and GetHashCode to get full value semantics.

public class TimeInterval
{
    private readonly DateTime _start;
    private readonly DateTime _end;

    public DateTime Start { get { return _start; } }
    public DateTime End { get { return _end; } }
    public TimeSpan Duration { get { return _end - _start; } }

    public TimeInterval(DateTime start, DateTime end)
    {
        if(end.Kind != start.Kind)
          throw new ArgumentException("Incompatible DateTimes");

        _start = start;
        _end = end;
    }

    public TimeInterval(DateTime start, TimeSpan duration)
    {
        _start = start;
        _end = start.Add(duration);
    }
}
0

精彩评论

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

关注公众号