分配数组 [英] allocating arrays

查看:58
本文介绍了分配数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我对编码很陌生,我只想澄清一下。

Hello, I am quite new to coding and I just want some clarification.

声明一个整数数组变量是什么意思,但是不要分配数组本身?

What does it mean to declare an integer array variable, but not to allocate the array itself?

我不明白它应该是什么样子

I don't understand what it is supposed to look like

谢谢!

推荐答案

在C#中,与许多现代语言一样,您的变量实际上并不是数组。 它是一个参考 - 基本上是数组的地址。 所以:

In C#, like many modern languages, your variable is not really the array.  It is a reference -- basically, the address of the array.  So:

    int [] iarray;

    int[] iarray;

声明一个名为"iarray"的变量能够引用数组。 但是,目前还没有可供参考的阵列。 它包含一个空引用 - 一个空引用。 尝试使用该数组将
导致异常。 当你说:

That declares a variable called "iarray" that is able to refer to an array.  At the current time, however, there is no array for it to reference.  It contains an empty reference -- a null reference.  Trying to use the array will cause an exception.  When you say:

    iarray = new int [] {1,2,3,4,5};

    iarray = new int[] { 1, 2, 3, 4, 5 };

创建一个包含5个元素的匿名数组对象,并使"iarray"成为"iarray"。变量引用该数组。 如果您现在说:

That creates an anonymous array object with 5 elements, and makes the "iarray" variable refer to that array.  If you now say:

    int [] jarray = iarray;

    int[] jarray = iarray;

这不会创建新数组。 相反,这两个变量都将引用相同的匿名数组对象。 如果你删除了iarray,那么数组对象仍然存在,只有一个名为"jarray"的引用。

That doesn't create a new array.  Instead, both of those variables will refer to the same anonymous array object.  If you delete iarray, the array object still exists, with a single reference called "jarray".

这是C#中的一个重要概念。 对象都是匿名的,生活在一个无名的大对象云中。 随着时间的推移,您的变量将引用这些无名对象,可能在程序继续时使用不同的名称。 这是
我们需要有垃圾收集器的原因 - 它会定期扫描大的无名对象云,寻找没有任何引用的对象。 可以真正删除这些对象。

This is an important concept in C#.  Objects are all anonymous, live in a big nameless object cloud.  Your variables will have references to those nameless objects over time, possibly with different names as the program goes on.  This is the reason why we need to have a garbage collector -- it periodically scans the big nameless object cloud looking for objects that don't have any references.  Such objects can be truly deleted.


这篇关于分配数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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