在 C# 中,生成返回查找列表或预加载静态列表更好吗?

发布于 2024-08-02 10:25:47 字数 1130 浏览 7 评论 0原文

我有一个简单的查找列表,我将用它来填充 Silverlight 中的下拉列表。在此示例中,我使用的是美国各州。

我试图弄清楚返回静态列表或使用 是否更好产量关键字。以下两段代码中,哪一个是首选,为什么?

版本 1: 使用收益回报

public class States
{
    public static IEnumerable<string> GetNames()
    {
        yield return "Alabama"; 
        yield return "Alaska";
        yield return "Arizona";
        yield return "Arkansas";
        yield return "California";
        yield return "Others ...";
    }
}

版本 2: 返回列表

public class States
{
    private static readonly IList<string> _names;

    static States()
    {
        _names = new List<string>() {"Alabama", 
                                     "Alaska",
                                     "Arizona",
                                     "Arkansas",
                                     "California",
                                     "Others ..." };

    }

    public static IList<string> GetNames()
    {
        return _names;
    }
}

I have a simple lookup list that I will use to populate a dropdown in Silverlight. In this example I'm using US States.

I'm trying to figure out if its better to return a static list or use the yield keyword. Of the following two pieces of code, which is the preferred and why?

Version 1: Using yield return

public class States
{
    public static IEnumerable<string> GetNames()
    {
        yield return "Alabama"; 
        yield return "Alaska";
        yield return "Arizona";
        yield return "Arkansas";
        yield return "California";
        yield return "Others ...";
    }
}

Version 2: Return the list

public class States
{
    private static readonly IList<string> _names;

    static States()
    {
        _names = new List<string>() {"Alabama", 
                                     "Alaska",
                                     "Arizona",
                                     "Arkansas",
                                     "California",
                                     "Others ..." };

    }

    public static IList<string> GetNames()
    {
        return _names;
    }
}

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

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

发布评论

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

评论(3

淡写薰衣草的香 2024-08-09 10:25:47

您需要问自己的问题是:“我是否希望调用 GetNames 的代码能够修改列表?”

  • 如果答案是肯定的,则返回一个 IList,隐式表示:“读写”
  • 如果答案是否定的,则返回一个 IEnumerable,隐式表示:“只读”

无论如何,我认为你应该将状态名称放在资源文件中,而不是对它们进行硬编码(即使该列表在短期内不太可能改变......)

这就是我要做的:

public static IEnummerable<string> GetNames()
{
    foreach(string name in _names) yield return name;
}

这样你就不会'不要直接向“外界”公开名单

The question you need to ask yourself is : "do I want the code that calls GetNames to be able to modify the list ?"

  • If the answer is yes, return a IList, implicitly saying : "read-write"
  • If the answer is no, return a IEnumerable, implicitly saying : "read-only"

Anyway, I think you should put the state names in a resource file, rather than hard-coding them (even though that list is unlikely to change in the short term...)

Here is what I would do :

public static IEnummerable<string> GetNames()
{
    foreach(string name in _names) yield return name;
}

This way you don't directly expose the list to the "outside world"

中二柚 2024-08-09 10:25:47

我想我更喜欢这个列表。与此处的 List 相比,yield 方法可能提供的唯一优点是避免需要一次将所有元素保留在内存中,但无论如何您都会这样做。

I think I'd prefer the list for this. The only advantage the yield method might offer over the List here is to avoid needing to keep all the elements in memory at once, but you're going to do that anyway.

囍孤女 2024-08-09 10:25:47

就个人而言,我更喜欢列表而不是迭代器,尽管我也会考虑将包含此类列表(可能是 XML,可能是纯文本)的资源嵌入到程序集中,并在静态构造函数中读取它。

Personally I'd prefer the List over the iterator, though I'd also look at embedding a resource containing such a list (perhaps XML, perhaps plain text) into your assembly and read that in your static constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文