總有一些最佳實踐等,但我不知道我的一切,每個人都已經表示同意...
如果我理解正確的話,你必須列舉你的嵌套數組幾個不同的原因和想封裝這個功能可以重複使用。有幾種不同的方式來實現,但這裏是一個使用LINQ和一個用了...
List<DateTime[]> changes = new List<DateTime[]>();
changes = PopulateChanges();
// *** As pointed out ForEach this will not change the value in the array, Thanks!***
changes.ForEach(change => change.ToList().ForEach(date => date = DateTime.MinValue));
// this how ever works but is kind of confusing...
changes = changes.Select(change => change.Select(date => DateTime.MinValue).ToArray()).ToList();
//Even that would get old writing it over and over again...
我會做這樣......
private void ForEachOnNestedDates(List<DateTime[]> list, Func<DateTime, DateTime> method)
{
// Use your code...
for (int i = 0; i < list.Count; i++)
{
for(int j = 0; j < list[i].Length; j++)
{
list[i][j] = method.Invoke(list[i][j]);
}
}
}
這將允許您重複使用並對每個項目執行不同的操作。恩。
//Add a Day to each datetime value
ForEachOnNestedDates(changes, (DateTime date) => date.AddDays(1));
//You also make it even more readable
ForEachOnNextedDates(changes, DoSomethingWithDate);
private DateTime DoSomethingWithDate(DateTime value)
{
}
這將用於完成每個日期時間的事情,而不是每個日期時間完成的事情......這將有所不同。
我喜歡這個。我想,足夠接近我。 – 2009-09-11 22:56:06