Click here to Skip to main content
15,917,568 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
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 ??
Posted

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.
 
Share this answer
 
Comments
fdiu 19-Oct-12 5:41am    
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??
fjdiewornncalwe 25-Oct-12 11:37am    
+5. Good explanation.
CPallini 25-Oct-12 13:18pm    
Thank you.
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:
C#
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:
C#
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:
C#
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
 
Share this answer
 
v5
Comments
fdiu 19-Oct-12 5:41am    
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??
fdiu 19-Oct-12 6:06am    
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
fdiu 19-Oct-12 6:33am    
Thanks for giving me new direction to think.

You suspect params to Set method
so i write one program to get all methods from
I found this method.
void Set(Int32,int32,Double) method
So we go in correct direction
fdiu 19-Oct-12 6:35am    
Thanks bro for consuming time for me
OriginalGriff 19-Oct-12 6:41am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900