What's the magic of arrays in C#

后端 未结 8 1082
生来不讨喜
生来不讨喜 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:41

    There is such class. You cannot inherit it, but when you write "int[]" the compiler creates a type that inherits System.Array. So if you declare a variable:

    int[] x;
    

    This variable will have a type that inherits System.Array, and therefore has all its methods and properties.

    This is also similar to delegates. When you define a delegate:

    delegate void Foo(int x);
    delegate int Bar(double x);
    

    Then the type Foo is actually a class that inherits System.MulticastDelegate and Bar is a class that inherits System.Delegate.

提交回复
热议问题