1. 程式人生 > 其它 >list foreach方法_Linq中帶有迭代索引的Select擴充套件方法,為啥知道的人不多呢?

list foreach方法_Linq中帶有迭代索引的Select擴充套件方法,為啥知道的人不多呢?

技術標籤:list foreach方法

一:背景

昨天在看C#函數語言程式設計這本書的時候,有一處讓我乾著急,需求是這樣:給多行文字加上數字列表項。

d69227bd15c5593b46f915856d563a3e.png

針對這個需求你會如何快捷高效的給每個專案加上數字編號呢?我看書中是這樣實現的,如下程式碼


public class Program
{
public static void Main(string[] args){
var list = new List<string>()
{
"cnblogs","csdn","zhihu","oschina"
};

var items = list.Zip(Enumerable.Range(1, list.Count + 1), (item, i) => $"{i}. {item}").ToList();

items.ForEach(Console.WriteLine);
}
}

------------------- output -------------------
1. cnblogs
2. csdn
3. zhihu
4. oschina
Press any key to continue . . .

怎麼說呢,需求能實現沒有問題,但這裡還是累贅了,因使用到了拉鍊函式Zip 和生成範圍的Range,全糾纏到一塊,有沒有更簡單粗暴的方式呢?其實你只用Select的一個帶迭代變數的過載方法就可以搞定,但現實中還是有很多的程式設計師不會使用或者說不知道,所以優化後的程式碼如下。


var items = list.Select((item, i) => $"{i + 1}. {item}").ToList();

------------------- output -------------------
1. cnblogs
2. csdn
3. zhihu
4. oschina
Press any key to continue . . .

二:原始碼探究

相信編碼多年的我們無數次的在憎恨foreach沒有帶上索引,這麼普羅大眾的需求盡然都沒有實現,而python這樣的語言早就給實現了,為了解決這個不足,我還得需要單獨定義一個變數在迭代時即時記錄,煩不勝煩,就像下面這樣。

            var index = 0;

foreach (var item in list)
{
index++;
Console.WriteLine(item);
}

可能FCL類庫程式設計師也深有體會,所以加了一個可以在迭代中獲取當前index的絕妙方法,這麼??造福人類的方法卻發現身邊知道的人少之又少,小遺憾哈。

1. ILSpy檢視原始碼

從下面程式碼的 SelectIterator 列舉類可以看到,其實在底層也是單獨給你定義了一個index,然後逐一回調給你的回撥函式,這種封裝很有必要,不過全是高階函式,功底很深哈!

public static IEnumerable Select(this IEnumerablesource, Funcint, TResult> selector)
{if (source == null)
{throw Error.ArgumentNull("source");
}if (selector == null)
{throw Error.ArgumentNull("selector");
}return SelectIterator(source, selector);
}private static IEnumerable SelectIterator(IEnumerablesource, Funcint, TResult> selector)
{int index = -1;
foreach (TSource item in source)
{
index = checked(index + 1);
yield return selector(item, index);
}
}

三:其他案例

其實有太多例子需要使用迭代器中的index值了,比如最近業務中要計算各個月份的環比率,用今天講到的這個方法就可以非常完美的解決,簡化後的程式碼如下。


public static void Main(string[] args){
var list = new List<int>()
{
10, 20, 30,40,50, 60,70,80,90,100,110,120,
};

var rateList = list.Select((item, index) =>
{
return index == 0 ? 0 : Math.Round(((decimal)list[index] - list[index - 1]) / list[index - 1], 2);
}).ToList();

rateList.ForEach(Console.WriteLine);
}

------------------- output -------------------
0
1
0.5
0.33
0.25
0.2
0.17
0.14
0.12
0.11
0.1
0.09
Press any key to continue . . .

好了,本篇來自於觸景生情,希望您編碼有幫助。


您的轉發是我寫作最大的動力