Linq、流媒体和收藏

发布于 2024-12-22 15:15:15 字数 1636 浏览 1 评论 0原文

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

    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,因此编写 this :

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

和 this :

    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 而不是 List 是否有任何兴趣或好处,因为此方法调用 ReturnsStringArray 返回字符串数组而不是 IEnumerable

I have this method which return an array of string :

    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"
        };
    }
}

I want to write a method which use this method. As this method returns an array and not a IEnumerable, is there the same result to write this :

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

and this :

    private static List<string> ParseStringArray()
    {
        return ReturnsStringArray()
            .Where(s => s.StartsWith("r"))
            .Select(c => c.ToUpper())
            .ToList(); // return a list instead of an IEnumerable.
    }

Thank you.

EDIT

My question is : Is there any interest or benefits that the method ParseStringArray() returns an IEnumerable instead of a List because of this method calls ReturnsStringArray that returns an array of string and not a IEnumerable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

护你周全 2024-12-29 15:15:15

当您返回 List 时,您是在说“所有处理都已完成,并且 List 包含结果”。

但是,当您返回 IEnumerable 时,您是在说“可能仍需要完成处理”。

在您的示例中,当您返回 IEnumerable 时,.Where.Select 尚未被处理。这称为“延迟执行”。
如果用户使用结果 3 次,则 .Where.Select 将执行 3 次。由此可能产生很多棘手的问题。

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

When you return a List, you are saying that "all processing has been done, and the List contains the results".

However, when you return an IEnumerable, you are saying that "processing might still need to be done".

In your example, when you return an IEnumerable, the .Where and .Select have NOT bee processed yet. This is known as "deferred execution".
If the user uses the result 3 times, then the .Where and .Select will execute 3 times. There are a good number of tricky issues that can come from this.

I recommend using a List as often as possible when returning values from a method. In addition to the additional functionality you'll get from a List, .NET has many optimizations that require a List, debugging support is better, and there's a reduced chance of unintended side effects!

黎夕旧梦 2024-12-29 15:15:15

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

1) IEnumerable 只是一个字符串序列,但 List 可以通过 int 索引进行索引,可以在特定索引处添加和删除项目以及插入项目。

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

A List is a concrete implementation of IEnumerable. The difference is that

1) IEnumerable is merely a sequence of string but a List is indexable by an int index, can be added to and removed from and have items inserted at a particular index.

2) An item can be iterated by a sequence, but doesn't allow random access. A List is a specific random-access variable-size collection.

-残月青衣踏尘吟 2024-12-29 15:15:15

我个人建议返回一个 string[],因为您不太可能希望添加到结果中(忽略 List),但看起来您可能想要顺序访问(IEnumerable 不适用)。是否推迟执行取决于您和情况;如果多次使用结果,则在返回结果之前调用 ToArray() 可能是明智之举。

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

I would personally recommend returning a string[], as you will unlikely wish to add to the results (dismissing List<string>), but it looks like you may want sequential access (which IEnumerable<string> is not intended for). Whether you defer execution or not is up to you and the situation; if the result is used many times, it may be wise to call ToArray() before returning the results.

private static string[] ParseStringArray()
{
    return ReturnsStringArray()
        .Where(s => s.StartsWith("r"))
        .Select(c => c.ToUpper())
        .ToArray();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文