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];
}
}
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
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
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