C#中的System.Array [英] System.Array in c#

查看:112
本文介绍了C#中的System.Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#中的数组实现感到惊讶.
int [] arr = new int [2];
"arr"如何显示System.Array类的方法和属性?.

我知道System.Array类是仅由编译器在内部实例化的,我们无法显式实例化它.

但是a.GetType().Name再次显示int []类型.即,一次保存对数组以及System.Array类的引用

I m amazed by seeing arrays implementation in c#.
int[] arr=new int[2];
how ''arr'' shows methods and properties of System.Array class?.

I know System.Array class is instantiated internally by only compiler we can not explicitly instantiate it.

But again a.GetType().Name shows int[] type.ie a holds reference to array as well as System.Array class at a time ??

推荐答案

据我所知,数组是System.Array s.或者,如文档文档所述更好地说明 [ Array类是支持数组的语言实现的基类. .

您可以使用
ildasm [
As far as I know, arrays are System.Arrays. Or, as better stated by the documentation documentation[^]:
The Array class is the base class for language implementations that support arrays.

You may use ildasm[^] tool for inspecting generated code and verify yourself.


int [x]是System.Array(x)的语法糖,并带有添加的"int".
System.Array是一个抽象类,它是C#中所有数组数据类型的基础,只是您使用C#特定语言构造对其进行了声明.

但是当我们访问int [2,2]时,
在System.Array中,对于带有两个参数的索引器来说,它们是没有实现的.那么它的值是怎么来的?

如果您创建一个简单程序:
int[x] is syntactic sugar for System.Array(x) with an added "int" thrown in.
System.Array is an abstract class that is the base for all array datatypes in C#, it is just that you declare it using C# specific language constructs.

"But when we access int[2,2] then
in System.Array their is no implementation for indexer with two parameters.Then how its value come from??"



If you create a simple program:
class Program
    {
    static void Main(string[] args)
        {
        int[,] arr = new int[2, 3];
        }
    }

并使用ILDASM检查EXE:

And examine the EXE with ILDASM:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       10 (0xa)
  .maxstack  2
  .locals init (int32[0...,0...] V_0)
  IL_0000:  nop
  IL_0001:  ldc.i4.2
  IL_0002:  ldc.i4.3
  IL_0003:  newobj     instance void int32[0...,0...]::.ctor(int32,
                                                             int32)
  IL_0008:  stloc.0
  IL_0009:  ret
} // end of method Program::Main

的结尾您将看到为二维数组定义了一个构造函数.
实际上,它很可能被定义为params int[] dimensions,以允许二维,三维等等.


好,我认为这是System.Array(params int [])的构造函数.
但我的问题实际上是这个,

int i = a [2,2];
然后这里a [2,2]返回一个整数值.
但是System.Array不包含带有两个参数的索引器,因此它如何返回值.然后它如何出现".



同样,解决方案是使用ILDASM进行查看:

You will see that a constructor is defined for two dimensional arrays.
In all probability it is actually defined as params int[] dimensions to allow for two dimensional, three dimensional, and so on.


"Good,I think this is an constructor for System.Array(params int[])
But my question is actually this,

int i=a[2,2];
then here a[2,2] return an integer value.
But System.Array not contain indexer with two parameters so how it returns value.Then how it comes"



Again, the solution is to look at it with ILDASM:

class Program
    {
    static void Main(string[] args)
        {
        int[,] arr = new int[4, 5];
        arr[2, 3] = 7;
        }
    }


.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       19 (0x13)
  .maxstack  4
  .locals init (int32[0...,0...] V_0)
  IL_0000:  nop
  IL_0001:  ldc.i4.4
  IL_0002:  ldc.i4.5
  IL_0003:  newobj     instance void int32[0...,0...]::.ctor(int32,
                                                             int32)
  IL_0008:  stloc.0
  IL_0009:  ldloc.0
  IL_000a:  ldc.i4.2
  IL_000b:  ldc.i4.3
  IL_000c:  ldc.i4.7
  IL_000d:  call       instance void int32[0...,0...]::Set(int32,
                                                           int32,
                                                           int32)
  IL_0012:  ret
} // end of method Program::Main

的结尾,同样,我会怀疑Set方法的params参数.

对于Get方法,结果类似:

Again, I would suspect a params parameter to the Set method.

And a similar result for the Get method:

class Program
    {
    static void Main(string[] args)
        {
        int[,] arr = new int[4, 5];
        arr[2, 3] = 7;
        int i = arr[2, 3];
        }
    }


.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       28 (0x1c)
  .maxstack  4
  .locals init ([0] int32[0...,0...] arr,
           [1] int32 i)
  IL_0000:  nop
  IL_0001:  ldc.i4.4
  IL_0002:  ldc.i4.5
  IL_0003:  newobj     instance void int32[0...,0...]::.ctor(int32,
                                                             int32)
  IL_0008:  stloc.0
  IL_0009:  ldloc.0
  IL_000a:  ldc.i4.2
  IL_000b:  ldc.i4.3
  IL_000c:  ldc.i4.7
  IL_000d:  call       instance void int32[0...,0...]::Set(int32,
                                                           int32,
                                                           int32)
  IL_0012:  ldloc.0
  IL_0013:  ldc.i4.2
  IL_0014:  ldc.i4.3
  IL_0015:  call       instance int32 int32[0...,0...]::Get(int32,
                                                            int32)
  IL_001a:  stloc.1
  IL_001b:  ret
} // end of method Program::Main


这篇关于C#中的System.Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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