Domanda
Come si ottiene il massimo datetime da un elenco di valori DateTime utilizzando C # 2.0?
Soluzione
Ecco un semplice ciclo per farlo:
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;
Altri suggerimenti
Che succede con tutte le iterazioni .... questo è molto banale
// Given...
List<DateTime> dates = { a list of some dates... }
// This is the max...
DateTime MaxDate = dates.Max();
Intendi datetime max in set, collection o List ? In tal caso:
DateTime max = DateTime.MinValue;
foreach (DateTime item in DateTimeList)
{
if (item > max) max = item;
}
return max;
Se vuoi dire che vuoi conoscere il valore supportato più alto possibile per qualsiasi datetime, è solo:
DateTime.MaxValue;
var max = new [] {datetime1, datetime2} .Max ();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow