Where are the int [] and string [] classes defined?

int a = 0; int[] b = new int[3]; Console.WriteLine( a.GetType() ); Console.WriteLine( b.GetType() ); 

type a is System.Int32 , which is a structure. type b is int[] .

I see an Int32 definition in Visual Studio. Where is the definition of int[] ?

+6
source share
3 answers

For a given type T type T[] predefined by composition. So T[][] , etc.

+8
source

System.Array

The Array class is the base class for language implementations that support arrays. However, only the system and compilers can get explicitly from the Array class. Users should use the array constructs provided by the language.


More details:

Starting with the .NET Framework 2.0, the Array class implements the generic interfaces System.Collections.Generic.IList, System.Collections.Generic.ICollection and System.Collections.Generic.IEnumerable. Implementations are provided to arrays at runtime , and therefore they are not visible to documentation assembly tools. As a result, common interfaces are not displayed 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 into a general interface type (explicit interface implementations). The key point to consider when creating an array on one of these interfaces is that members who add, insert, or delete elements throw a NotSupportedException.

+4
source

WITH#:

 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 the detailed information about newarr http://www.dotnetperls.com/newarr

The newarr instruction is not very interesting. But it is an important .NET Framework design solution. Vectors (1D arrays) are separated from 2D arrays. And this knowledge can influence the types that you choose in programs.

+2
source

All Articles