Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 林奇,流媒体及;收集_C#_Linq_Collections_Stream - Fatal编程技术网

C# 林奇,流媒体及;收集

C# 林奇,流媒体及;收集,c#,linq,collections,stream,C#,Linq,Collections,Stream,我有一个返回字符串数组的方法: 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",

我有一个返回字符串数组的方法:

    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静态IEnumerable ParseStringArray()
{
returnsStringaray()
.其中(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()
{
returnsStringaray()
.其中(s=>s.StartsWith(“r”))
.Select(c=>c.ToUpper())
.ToList();//返回列表而不是IEnumerable。
}
多谢各位

编辑


我的问题是:ParseStringArray()方法返回IEnumerable而不是List有什么好处吗?因为这个方法调用了ReturnsStringArray,它返回一个字符串数组而不是IEnumerable当你返回一个
List
,你是说“所有处理均已完成,列表中包含结果”

但是,当您返回一个
IEnumerable
时,您的意思是“可能仍然需要进行处理”

在您的示例中,当您返回一个
IEnumerable
时,
.Where
.Select
尚未处理。这称为“延迟执行”。
如果用户使用结果3次,则
.Where
.Select
将执行3次。这可能会导致许多棘手的问题


我建议在从方法返回值时尽可能多地使用
列表
。除了从
列表
中获得的附加功能外,.NET还有许多需要
列表
的优化功能,调试支持更好,并且在重新启动时,意外副作用的可能性更低!

urn a
列表
,您的意思是“所有处理都已完成,列表包含结果”

但是,当您返回一个
IEnumerable
时,您的意思是“可能仍然需要进行处理”

在您的示例中,当您返回一个
IEnumerable
时,
.Where
.Select
尚未处理。这称为“延迟执行”。
如果用户使用结果3次,则
.Where
.Select
将执行3次。这可能会导致许多棘手的问题


我建议在从方法返回值时尽可能多地使用
列表
。除了从
列表
中获得的附加功能外,.NET还有许多需要
列表
的优化,调试支持更好,并且减少意外副作用的可能性!

列表是IEnumerable的具体实现。区别在于

1) IEnumerable只是一个字符串序列,但列表可以通过int索引进行索引,可以添加到特定索引中,也可以从中删除,还可以在特定索引中插入项


2) 一个项目可以通过序列进行迭代,但不允许随机访问。列表是一个特定的随机访问变量大小集合。

列表是IEnumerable的具体实现。区别在于

1) IEnumerable只是一个字符串序列,但列表可以通过int索引进行索引,可以添加到特定索引中,也可以从中删除,还可以在特定索引中插入项


2) 一个项目可以按序列进行迭代,但不允许随机访问。列表是一个特定的随机访问变量大小集合。

我个人建议返回一个
字符串[]
,因为您不太可能希望添加到结果中(取消
列表
),但看起来您可能需要顺序访问(IEnumerable不适用于它)。是否推迟执行取决于您和具体情况;如果结果被多次使用,在返回结果之前调用
ToArray()
可能是明智的

private static string[] ParseStringArray()
{
    return ReturnsStringArray()
        .Where(s => s.StartsWith("r"))
        .Select(c => c.ToUpper())
        .ToArray();
}

我个人建议返回一个
字符串[]
,因为您不太可能希望添加到结果中(取消
列表
),但看起来您可能需要顺序访问(这不是
IEnumerable
的目的)。是否推迟执行取决于您和具体情况;如果结果被多次使用,则最好在返回结果之前调用
ToArray()

private static string[] ParseStringArray()
{
    return ReturnsStringArray()
        .Where(s => s.StartsWith("r"))
        .Select(c => c.ToUpper())
        .ToArray();
}

问题是什么?您提供的两个函数返回相同的结果。“正直的人四面受困”,但大写。问题是什么?您提供的两个函数返回相同的结果。“正直的人四面受困”,但大写。