C# - 为什么你需要实例每个数组元素? [英] C# - Why do you need to instantiate each array element?

查看:87
本文介绍了C# - 为什么你需要实例每个数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用这种类型的构造。

I use this type of construct often.

response = new LocationResponse ();
response.LocationDetails = new LocationDetail[4];
response.LocationDetails[0] = new LocationDetail();
response.LocationDetails[0].site = "ABCDE";
...

这件作品,我不完全理解为这片:

The piece I don't fully understand is this piece:

response.LocationDetails [0] =新LocationDetail();

为什么阵列的每个元素都被实例

Why does each individual element of the array have to be instantiated?

如果你离开它,你得到了一个未定义的例外。

If you leave it out, you get undefined exceptions.

推荐答案

那么,如果一个数组元素被自动实例,你就被锁定在哪些类你定义的数组。你就不能够使用抽象类或接口或不具有一个默认的构造的任何对象的阵列。这仅仅是命名在一个阵列自动实例化对象的几个问题。

Well if an array elements were automatically instantiated, you would be locked into which classes you defined the array with. You wouldn't be able to use arrays of abstract classes or interfaces or any object which doesn't have a default constructor. This is just naming a few problems with automatically instantiating objects in an array.

这篇关于C# - 为什么你需要实例每个数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆