【发布时间】:2016-05-16 20:01:45
【问题描述】:
为什么给List赋值必须用Add,而给数组赋值可以用[]操作符?
例如:
string[] y = new string[10];
y[0] = "asdf"; //fine
List<string> x = new List<string>(10);
x[0] = "asdf"; //ArgumentOutOfRangeException
两者不应该有相同的行为吗?
【问题讨论】:
-
Shouldn't both have the same behavior?没有。为什么应该是一样的?它们不是一回事...... -
您需要先将字符串“asdf”添加到x,然后才能使用它。 x.add("asdf")。现在列表是空的。
-
你为什么认为他们会有相同的行为?列表和数组是不同的东西。
-
我假设 c#
List与 c++vector相同。 -
@kuhaku 这是相同的想法,但实现不必相同。
标签: c# arrays collections syntax