我有這樣的方法,該方法返回一個字符串數組:LINQ中,流媒體和收集
private static string[] ReturnsStringArray()
{
return new string[]
{
"The path of the ",
"righteous man is beset on all sides",
"by the inequities of the selfish and",
"the tyranny of evil men. Blessed is",
"he who, in the name of charity and",
"good will, shepherds the weak through",
"the valley of darkness, for he is",
"truly his brother's keeper and the",
"finder of lost children. And I will ",
"strike down upon thee with great",
"vengeance and furious anger those",
"who attempt to poison and destroy my",
"brothers. And you will know my name",
"is the Lord when I lay my vengeance",
"upon you"
};
}
}
我希望編寫使用此方法的方法。由於該方法返回一個數組,而不是一個IEnumerable的,有相同的結果寫這篇文章:
private static IEnumerable<string> ParseStringArray()
{
return ReturnsStringArray()
.Where(s => s.StartsWith("r"))
.Select(c => c.ToUpper());
}
這:
private static List<string> ParseStringArray()
{
return ReturnsStringArray()
.Where(s => s.StartsWith("r"))
.Select(c => c.ToUpper())
.ToList(); // return a list instead of an IEnumerable.
}
謝謝。
編輯
我的問題是:是否有任何利益或好處的方法ParseStringArray()返回,因爲這種方法的IEnumerable,而不是一個名單打電話ReturnsStringArray返回字符串數組,而不是一個IEnumerable的
什麼問題?你提供的兩個函數都返回相同的結果。 「義人在四面八方」,但大寫。 – NoLifeKing 2011-12-23 09:21:56