List class properties in C #

I have a class like this

 public class Model
{
    public TimeSpan Time1 {get; set;}
    public TimeSpan Time2 { get; set; }
    public TimeSpan Time3 { get; set; }
    public TimeSpan Time4 { get; set; }

}

Now imagine that I have to fill in the time at runtime, and then figure out the time left between time 1 and time 2, then when that will pass. Find the time left between Time2 and Time3, and so on. However, I need to take into account what time it is.

Example:

Now 1:00 PM

Time1 = 5:00 Time 2 = 12:00 Time 3 = 4:00 pm Time 4 = 6:00 pm

So, since the time is 1:00 PM, I need to find the difference between Time 2 and Time 3

Now is there a smarter way than thinking to define it? Should I add something in my class

+3
source share
6 answers

, :

public class Model
{
    public TimeSpan Time1 {get; set;}
    public TimeSpan Time2 { get; set; }
    public TimeSpan Time3 { get; set; }
    public TimeSpan Time4 { get; set; }

    public IEnumerable<TimeSpan> GetTimes()
    {
        yield return Time1;
        yield return Time2;
        yield return Time3;
        yield return Time4;
    }
}

:

foreach (TimeSpan time in model.GetTimes())
{
    // use the TimeSpan
}
+7

?

public class Model
{
    public List<DateTime> Dates { get; set; }
}
+3

, Dictionary<TimeSpan, string>, TimeSpan, , . "Time1"? , , .

+1

, . , . , , , , .

0

, , , .

public class Model
{
    private TimeSpan[] _timeSpans = new TimeSpan[4] { new TimeSpan(), new TimeSpan(), new TimeSpan(), new TimeSpan() };

    public TimeSpan Time1
    {
        get { return _timeSpans[0]; }
        set { _timeSpans[0] = value; }
    }
    public TimeSpan Time2
    {
        get { return _timeSpans[1]; }
        set { _timeSpans[1] = value; }
    }
    public TimeSpan Time3
    {
        get { return _timeSpans[2]; }
        set { _timeSpans[2] = value; }
    }
    public TimeSpan Time4
    {
        get { return _timeSpans[3]; }
        set { _timeSpans[3] = value; }
    }

    // DateTime.TimeOfDay holds the time portion of a time
    public TimeSpan GetDifference(TimeSpan currentTime)
    {
        int start = -1;
        for(int i = 0; i<_timeSpans.Length;i++)
        {
            if(_timeSpans[i] >= currentTime)
            {
                start = i;
                break;
            }
        }
        if(start == -1) throw new ArgumentException("handle the case where currentTime is smaller than all of them");
        int end = (start + 1 < _timeSpans.Length) ? start + 1 : 0;
        return _timeSpans[end] - _timeSpans[start];
    }
0
source

Array construction provides a simple linq operator for calculating the time interval:

public class Model
{
    public TimeSpan Time1 { get; set; }
    public TimeSpan Time2 { get; set; }
    public TimeSpan Time3 { get; set; }
    public TimeSpan Time4 { get; set; }

    public TimeSpan GetSpan(TimeSpan time)
    {
        var times = new[] { Time1, Time2, Time3, Time4 };

        return Enumerable.Range(1, times.Length - 1)
            .Select(i => new[] { times[i - 1], times[i] })
            .Where(t => t[0] <= time && time < t[1])
            .Select(t => t[1] - t[0])
            .FirstOrDefault();
    }
}
0
source

Source: https://habr.com/ru/post/1738593/


All Articles