C# 如何创建 100 个新对象的数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10210024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create array of 100 new objects?
提问by Patryk
I'm trying something like that:
我正在尝试这样的事情:
class point
{
public int x;
public int y;
}
point[] array = new point[100];
array[0].x = 5;
and here's the error: Object reference not set to an instance of an object. (@ the last line)
这是错误: 未将对象引用设置为对象的实例。(@最后一行)
whats wrong? :P
怎么了?:P
采纳答案by Skalli
It only creates the array, but all elements are initialized with null.
You need a loop or something similar to create instances of your class.
(foreach loops dont work in this case)
Example:
它只创建数组,但所有元素都用 null 初始化。
您需要一个循环或类似的东西来创建类的实例。(foreach 循环在这种情况下不起作用)示例:
point[] array = new point[100];
for(int i = 0; i < 100; ++i)
{
array[i] = new point();
}
array[0].x = 5;
回答by Paolo Tedesco
When you do
当你做
point[] array = new point[100];
you create an array, not 100 objects. Elements of the array are null. At that point you have to create each element:
您创建一个数组,而不是 100 个对象。数组的元素为空。那时你必须创建每个元素:
array[0] = new point();
array[0].x = 5;
回答by THX-1138
You can change class pointto struct pointin that case new point[500]will create an array of points initialized to 0,0(rather than array of null's).
在这种情况下,您可以更改class point为将创建一个初始化为的点数组(而不是空数组)。struct pointnew point[500]0,0
回答by Ohad Schneider
As the other answers explain, you need to initialize the objects at each array location. You can use a method such as the following to create pre-initialized arrays
正如其他答案所解释的那样,您需要在每个数组位置初始化对象。您可以使用如下方法创建预初始化数组
T[] CreateInitializedArray<T>(int size) where T : new()
{
var arr = new T[size];
for (int i = 0; i < size; i++)
arr[i] = new T();
return arr;
}
If your class doesn't have a parameterless constructor you could use something like:
如果您的类没有无参数构造函数,您可以使用以下内容:
T[] CreateInitializedArray<T>(int size, Func<T> factory)
{
var arr = new T[size];
for (int i = 0; i < size; i++)
arr[i] = factory();
return arr;
}
LINQ versions for both methods are trivial, but slightly less efficient I believe
这两种方法的 LINQ 版本都很简单,但我相信效率稍低
回答by Charp
int[] asd = new int[99];
for (int i = 0; i < 100; i++)
asd[i] = i;
Something like that?
类似的东西?
回答by Palec
Sometimes LINQ comes in handy. It may provide some extra readability and reduce boilerplate and repetition. The downside is that extra allocations are required: enumerators are created and ToArray does not know the array size beforehand, so it might need to reallocate the internal buffer several times. Use only in code whose maintainability is much more critical than its performance.
有时 LINQ 会派上用场。它可以提供一些额外的可读性并减少样板和重复。缺点是需要额外的分配:创建了枚举器并且 ToArray 事先不知道数组大小,因此可能需要多次重新分配内部缓冲区。仅在可维护性比其性能更重要的代码中使用。
using System.Linq;
const int pointsCount = 100;
point[] array = Enumerable.Range(0, pointsCount)
.Select(_ => new point())
.ToArray()

