使用String[]相对于List有哪些好处?

26 浏览
0 Comments

使用String[]相对于List有哪些好处?

这个问题已经有了答案:

可能的重复问题:

c#数组 vs 泛型列表

数组 vs List:何时使用哪个?

我知道使用List<>有很多好处。但是,我想知道使用数组可能仍然存在的好处。

谢谢。

admin 更改状态以发布 2023年5月22日
0
0 Comments

数组比列表更具有协变性(covariance)的优点。 \n

    class Person { /* ... */}
    class Employee : Person {/* ... */}
    void DoStuff(List people) {/* ... */}
    void DoStuff(Person[] people) {/* ... */}
    void Blarg()
    {
        List employeeList = new List();
        // ...
        DoStuff(employeeList); // this does not compile
        int employeeCount = 10;
        Employee[] employeeArray = new Employee[employeeCount];
        // ...
        DoStuff(employeeArray); // this compiles
    }

0
0 Comments

你将拥有一个简单的静态结构来保存物品,而不是使用列表(动态调整大小,插入逻辑等)带来的额外开销。

然而,在大多数情况下,这些好处被列表的灵活性和适应性所抵消。

0