使用默认构造函数初始化数组
public class Sample
{
static int count = 0;
public int abc;
public Sample()
{
abc = ++Sample.count;
}
}
我想创建一个上述类的数组,并希望通过调用默认构造函数来初始化数组中的每个元素,以便每个元素可以有不同的 abc
。所以我这样做了:
Sample[] samples = new Sample[100];
但这并不不做我认为应该做的事。看来默认构造函数没有被调用。创建数组时如何调用默认构造函数?
我也想知道上面的语句有什么作用?
public class Sample
{
static int count = 0;
public int abc;
public Sample()
{
abc = ++Sample.count;
}
}
I want to create an array of above class, and want each element in the array to be initialized by invoking the default constructor, so that each element can have different abc
.So I did this:
Sample[] samples = new Sample[100];
But this doesn't do what I think it should do. It seems this way the default constructor is not getting called. How to invoke default constructor when creating an array?
I also would like to know what does the above statement do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
基本上你不能。当您创建数组时,它总是最初填充类型的默认值 - 对于类而言,该默认值始终是空引用。对于
int
来说,它是 0,对于bool
来说,它是 false,等等。(如果您使用数组初始值设定项,这将创建“空”数组,然后然后 当然,用您指定的值填充它。)
通过调用构造函数来填充数组的方法有多种 - 我自己可能只使用 foreach 循环。将 LINQ 与 Enumerable.Range/Repeat 结合使用感觉有点强迫。
当然,您始终可以编写自己的填充方法,甚至作为扩展方法:
然后您可以使用:
我喜欢这个解决方案的地方:
当然您可以添加更多选项:
Func
代替的重载Func
的,将索引传递给提供者You can't, basically. When you create an array, it's always initially populated with the default value for the type - which for a class is always a null reference. For
int
it's 0, forbool
it's false, etc.(If you use an array initializer, that will create the "empty" array and then populate it with the values you've specified, of course.)
There are various ways of populating the array by calling the constructor - I would probably just use a foreach loop myself. Using LINQ with Enumerable.Range/Repeat feels a little forced.
Of course, you could always write your own population method, even as an extension method:
Then you could use:
What I like about this solution:
Of course you could add more options:
Func<int, T>
instead of aFunc<T>
, passing the index to the provider您的代码仅创建数组,但不创建其中的任何项目。基本上,您需要将
Sample
的实例存储到到此数组中。简而言之,没有任何花哨的 LINQ 等:
另请注意,您的解决方案不是线程安全的。
Your code creates only the array, but neither of its items. Basically, you need to store instances of
Sample
into this array.To put it simple, without any fancy LINQ etc.:
Please also note your solution is not thread-safe.
没有办法自动执行此操作;数组初始化本质上就是“将这块内存擦除为0”。你必须做类似的事情:
There is no way to do this automatically; array initialization is essentially "wipe this block of memory to 0s". You would have to do something like:
这是另一个不需要任何扩展方法的单行:
另一个不错的选择是 Scott 的对乔恩的回答的建议:
所以你可以这样做:
Here is another one-liner that doesn't require any extension method:
Another nice option is Scott's suggestion to Jon's answer:
So you can do:
此时你有一个大小为 100 的空数组,如果你想用项目填充它,那么你必须执行以下操作:
At this point you have an empty array of size 100, if you want to fill it with items, then you would have to do something like:
问题在于,通过声明该数组,您从未为每个对象分配空间。您仅仅为 100 个 Sample 类型的对象分配了空间。您必须自己调用每个构造函数。
详细说明:
一个有趣的解决方法可能是工厂函数。考虑将其附加到您的 Sample 类中。
隐藏一点瑕疵 - 前提是这对您来说是一个有用的功能。
The problem is that by declaring that array, you never allocated space for each object. You merely allocated space for 100 objects of type Sample. You'll have to call the constructor on each yourself.
To elaborate:
An interesting work around might be a factory function. Consider attaching this to your Sample class.
Hides the blemish, a bit - providing this is a useful function to you.