What's the magic of arrays in C#

后端 未结 8 1113
生来不讨喜
生来不讨喜 2020-12-09 08:56
int[] a = new int[5];
string[] b = new string[1];

The types of both a and b inherit from the abstract System.Array<

8条回答
  •  一个人的身影
    2020-12-09 09:38

    Look at the Array class.

    When declaring an array using the [] syntax, the compiler, behind the scenes will use this class for you.

    For C#, [] becomes a type that inherits from System.Array.

    From the C# 4.0 spec:

    §12.1.1 The System.Array type

    The type System.Array is the abstract base type of all array types. An implicit reference conversion (§6.1.6) exists from any array type to System.Array, and an explicit reference conversion (§6.2.4) exists from System.Array to any array type. Note that System.Array is not itself an array-type. Rather, it is a class-type from which all array-types are derived.

提交回复
热议问题