c#-数组从哪里继承(即.int []) [英] c# - where do arrays inherit from (i.e .int[] )

查看:122
本文介绍了c#-数组从哪里继承(即.int [])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建数组(如int [])时,它是否继承自任何东西?我以为它可能继承自System.Array,但是在查看编译后的CIL后,它似乎不是这样。

When creating an array, such as int[], does this inherit from anything? I thought it might inherit from System.Array, but after looking at the compiled CIL it does't appear so.

我认为它可能继承自System.Array或其他方式similair,考虑到您可以在数组上调用方法和访问属性。

I thought it might inherit from System.Array or somehting similair, considering you can call methods and access properties on an array.

Ie

int[] arr = {1, 2};
arr.Initialize();
arr.Length;


推荐答案

所有数组均源自 System.Array 。摘自(公认的古老) MSDN杂志的版本


所有数组类型都隐式派生自System.Array,而System.Array本身又派生自System.Object。这意味着所有数组始终是在托管堆上分配的引用类型,并且您的应用程序的变量包含对该数组的引用,而不是对该数组本身的引用。

All array types are implicitly derived from System.Array, which itself is derived from System.Object. This means that all arrays are always reference types which are allocated on the managed heap, and your app's variable contains a reference to the array and not the array itself.

C#语言规范(重点是我的):


类型System.Array是所有数组的抽象基类型类型。从任何数组类型到System.Array以及由
System.Array实现的任何接口类型都存在隐式引用转换
(第13.1.4节)。存在从System.Array以及System.Array实现的任何接口
类型到任何数组类型的显式引用转换(第13.2.3节)。 System.Array本身不是数组类型。而是
,它是一个类类型,所有数组类型都源自该类类型

The type System.Array is the abstract base type of all array types. An implicit reference conversion (§13.1.4) exists from any array type to System.Array and to any interface type implemented by System.Array. An explicit reference conversion (§13.2.3) exists from System.Array and any interface type implemented by System.Array to any array type. System.Array is not itself an array-type. Rather, it is a class-type from which all array-types are derived.

这篇关于c#-数组从哪里继承(即.int [])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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