使用 String[] 相对于 List有什么好处?

发布于 2024-09-02 00:11:40 字数 357 浏览 4 评论 0原文

可能的重复:
C# 数组与泛型列表
数组与列表:何时使用哪个?

我明白使用 List<> 有几个好处。但是,我想知道使用数组仍然存在哪些好处。

谢谢。

Possible Duplicates:
c# array vs generic list
Array versus List<T>: When to use which?

I understand that there are several benefits of using List<>. However, I was wondering what benefits might still exist for using arrays.

Thanks.

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

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

发布评论

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

评论(5

撩发小公举 2024-09-09 00:11:40

您将有一个简单的静态结构来保存项目,而不是与列表相关的开销(动态调整大小、插入逻辑等)。

但在大多数情况下,列表的灵活性和适应性比这些好处更重要。

You'll have a simple static structure to hold items rather than the overhead associated with a List (dynamic sizing, insertion logic, etc.).

In most cases though, those benefits are outweighed by the flexibility and adaptability of a List.

无人问我粥可暖 2024-09-09 00:11:40

数组相对于列表的一件事是协方差

    class Person { /* ... */}

    class Employee : Person {/* ... */}

    void DoStuff(List<Person> people) {/* ... */}

    void DoStuff(Person[] people) {/* ... */}

    void Blarg()
    {
        List<Employee> employeeList = new List<Employee>();
        // ...
        DoStuff(employeeList); // this does not compile

        int employeeCount = 10;
        Employee[] employeeArray = new Employee[employeeCount];
        // ...
        DoStuff(employeeArray); // this compiles
    }

One thing arrays have over lists is covariance.

    class Person { /* ... */}

    class Employee : Person {/* ... */}

    void DoStuff(List<Person> people) {/* ... */}

    void DoStuff(Person[] people) {/* ... */}

    void Blarg()
    {
        List<Employee> employeeList = new List<Employee>();
        // ...
        DoStuff(employeeList); // this does not compile

        int employeeCount = 10;
        Employee[] employeeArray = new Employee[employeeCount];
        // ...
        DoStuff(employeeArray); // this compiles
    }
墨离汐 2024-09-09 00:11:40

数组比列表更简单,因此开销更少。如果您只需要数组的功能,则没有理由使用列表。

数组是最简单的集合形式,大多数其他集合都以某种形式使用。列表实际上在内部使用数组来保存其项目。

每当语言构造需要轻量级的一次性集合时,它就会使用数组。例如这段代码:

string s = "Age = " + age.ToString() + ", sex = " + sex + ", location = " + location;

实际上在幕后变成了这段代码:

string s = String.Concat(new string[] {
  "Age = ",
  age.ToString(),
  ", sex = ",
  sex,
  ", location = ",
  location
});

An array is simpler than a List, so there is less overhead. If you only need the capabilities of an array, there is no reason to use a List instead.

The array is the simplest form of collection, that most other collections use in some form. A List actually uses an array internally to hold it's items.

Whenever a language construct needs a light weight throw-away collection, it uses an array. For example this code:

string s = "Age = " + age.ToString() + ", sex = " + sex + ", location = " + location;

actually becomes this code behind the scene:

string s = String.Concat(new string[] {
  "Age = ",
  age.ToString(),
  ", sex = ",
  sex,
  ", location = ",
  location
});
深海少女心 2024-09-09 00:11:40

如果您的集合是不可变的,我只会使用数组。

编辑:不可变在某种意义上你的收藏不会增长或缩小。

I would only use an array if your collection is immutable.

Edit: Immutable in a sense that your collection will not grow or shrink.

七禾 2024-09-09 00:11:40

速度将是主要的好处,除非您开始编写自己的代码来插入/删除项目等。

列表在内部使用数组,并且只管理列表在内部为您做的所有事情。

Speed would be the main benefit, unless you start writing your own code to insert/remove items etc.

A List uses an array internally and just manages all of the things a list does for you internally.

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