在C#中声明集合

jos*_*ose 1 c# collections

我的问题是:对于大量数据,最有效和最正确的是什么?

 _pointBuffer1 = new Point3DCollection {  
            new Point3D(140.961, 142.064, 109.300), new Point3D(142.728, 255.678, (...)
Run Code Online (Sandbox Code Playgroud)

- 要么 -

_pointBuffer1.Add(new Point3D(140.961, 142.064, 109.300)); _poitBuffer1.Add(142.728, (...)
Run Code Online (Sandbox Code Playgroud)

还是一样吗?

Point3D被声明为Point3DCollection,但我的问题是任何对象集合(例如可能是Int32).

Jon*_*eet 7

为了清楚起见,我强烈建议使用集合初始化程序(尽管我也使用了一些换行符).

他们不结束一样IL,介意你.第一个最终相​​当于:

var tmp = new Point3DCollection();
tmp.Add(new Point3D(140.961, 142.064, 109.300));
tmp.Add(new Point3D(142.728, 255.678));
...
_pointBuffer1 = tmp;
Run Code Online (Sandbox Code Playgroud)

换句话说,最终变量的赋值仅所有Add调用之后进行.

如果您的Point3D构造函数以某种方式引用,这很重要_pointBuffer1!