Csharp数组相关问题 [英] Csharp Array related question

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

问题描述

最近在一次访谈中,我被问到一个关于csharp数组的问题如下:



//定义了一个整数数组



var arr = int a [100];



现在,问题是 - 这个数组(arr)是否存储为一个对象(100个整数)或内存中的100个对象?

Recently in one of the interviews, I was asked a question about csharp arrays as following:

//an array of integers is defined

var arr = int a[100];

now, the question is - is this array(arr) stored as one object (of 100 integers) or 100 objects within memory ?

推荐答案

首先,没有C#array这样的东西!这是一个.NET数组。您需要了解.NET的作用和主要思想(在申请.NET开发人员之前)。



它可以正确写成

First of all, there is no such thing as "C# array"! This is a .NET array. You need to understand the role and main ideas of .NET (before applying for a job as .NET developer).

It could be correctly written as
int[] array = new int[100];





创建一个数组,在堆上分配所需的内存, array 变量或某个类/结构成员成为对数组对象的引用。数组对象本身存储100个原始 int 对象,100个整数,而不是100个对其他东西的引用。 (你看,这个问题引用了术语对象,对它的理解取决于一些文化或惯例。在这种情况下,我传统上将对象称为在运行时存在的所有内容,并在内存中占有一席之地,而不仅仅是OOP 对象。)



但这不是全部。数组本身不是原始对象,它是面向对象的引用类型,因此它的实例包含其他内容,这部分依赖于实现。如果检查调试器下的变量,您可以看到系统中的内容。很明显,它特别是存储了如数组对象大小这样重要的数据。



-SA



That creates an array allocating required memory on heap and array variable or some class/structure member becomes a reference to the array object. The array object itself does store 100 primitive int objects, 100 integers, not 100 references to something else. (You see, the question refers the term "object", and the understanding of it depends on some culture or conventions. In this context, I traditionally call "object" everything which exists during runtime and takes a place in memory, not only OOP "objects".)

But this is not all. Array itself is not a primitive object, it's an object-oriented reference type, so its instance holds something else, which is partially implementation-dependent. You can see what it is on your system if you inspect the variable under the debugger. Quite apparently, it, in particular, stores such an important piece of data as the size of the array object.

—SA


它令人困惑,不仅因为语法错误,而且技术上答案是两者。



如果你在谈论它是怎么回事在内存中处理,它是在内存中表示的100个整数的单个连续块。这一点很明显, Array.Resize [ ^ ]方法有效。如果它是100个单独的对象,则在调整大小时不必重新分配整个数组。



但是,内存中有100个单独的整数,所以在这方面它不是一个对象。数组都派生自System.Array类,甚至是您在上面定义的方式,因此这是一个单独的对象。它基本上是一个包含对象集合的单个对象,因此技术上答案都是。 System.Array封装了100个单独的项目。
Its confusing, not only because of the syntax errors, but technically the answer is both.

If you are talking about how its treated in memory, its a single continuous block of 100 integers represented in memory. This is apparent by how the Array.Resize[^] method works. If it was 100 individual "objects" then the entire array would not have to be reallocated when it is resized.

It is, however, 100 individual integers in memory, so in that respect its not a single "object". Arrays all derive from the System.Array class, even how you defined it above, so that's a single object. Its basically a single object that holds a collection of objects, so the answer technically is both. System.Array encapsulates the 100 individual items.


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

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