使用String[]相对于List有哪些好处?
使用String[]相对于List有哪些好处?
这个问题已经有了答案:
可能的重复问题:
我知道使用List<>有很多好处。但是,我想知道使用数组可能仍然存在的好处。
谢谢。
admin 更改状态以发布 2023年5月22日
数组比列表更具有协变性(covariance)的优点。 \n
class Person { /* ... */} class Employee : Person {/* ... */} void DoStuff(Listpeople) {/* ... */} 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 }