问题描述
我对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 ??
推荐答案
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!