我有这样的方法,该方法返回一个字符串数组: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