문제

C# 2.0을 사용하여 DateTime 값 목록에서 MAX DateTime을 어떻게 얻습니까?

도움이 되었습니까?

해결책

다음은 간단한 루프입니다.

List<DateTime> dates = new List<DateTime> { DateTime.Now, DateTime.MinValue, DateTime.MaxValue };

DateTime max = DateTime.MinValue; // Start with the lowest value possible...
foreach(DateTime date in dates)
{
    if (DateTime.Compare(date, max) == 1)
        max = date;
}

// max is maximum time in list, or DateTime.MinValue if dates.Count == 0;

다른 팁

모든 반복은 무엇입니까 .... 이것은 매우 사소한 일입니다.

// Given...
List<DateTime> dates = { a list of some dates... }

// This is the max...
DateTime MaxDate = dates.Max();

Max DateTime을 의미합니까? 세트, 수집 또는 목록? 그렇다면:

DateTime max = DateTime.MinValue;
foreach (DateTime item in DateTimeList)
{
    if (item > max) max = item;
}
return max;

당신이 모든 dateTime에 대해 가능한 가장 높은 지원 값을 알고 싶다는 것을 의미한다면, 그것은 단지 다음과 같습니다.

DateTime.MaxValue;

var max = new [] {dateTime1, dateTime2} .max ();

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top