int a = 0;
int[] b = new int[3];
Console.WriteLine( a.GetType() );
Console.WriteLine( b.GetType() );
the type of a is System.Int32
which is a struct.
the type of b is int[]
.
I can see the definition of Int32
in Visual Studio. Where is the definition of int[]
located?
For a given type T
, the type T[]
is predefined, by composition. So are T[][]
, etc.
System.Array
The Array class is the base class for language implementations that support arrays. However, only the system and compilers can derive explicitly from the Array class. Users should employ the array constructs provided by the language.
More detailed:
Starting with the .NET Framework 2.0, the Array class implements the System.Collections.Generic.IList, System.Collections.Generic.ICollection, and System.Collections.Generic.IEnumerable generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear in the declaration syntax for the Array class, and there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException.
C#:
static void Main(string[] args)
{
int a = 0;
int[] b = new int[3];
}
IL:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 11 (0xb)
.maxstack 1
.locals init ([0] int32 a,
[1] int32[] b)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: ldc.i4.3
IL_0004: **newarr** [mscorlib]System.Int32
IL_0009: stloc.1
IL_000a: ret
}
you can see "newarr" Here is a detail about newarr http://www.dotnetperls.com/newarr
The newarr instruction is not greatly interesting. But it does expose an important design decision of the .NET Framework. Vectors (1D arrays) are separate from 2D arrays. And this knowledge can influence the types you choose in programs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With