What is the open generic type of the array []?

[亡魂溺海] 提交于 2019-12-12 12:34:38

问题


When I do int[], string[], T[] - this is a generic array. An array is just an object like everything else.

So what is the actual open generic type of []? I assume it is just some syntactic sugar over something like Array<> but I haven't been able to find anything of the sort.

Bonus points if you can somehow answer this before Jon Skeet.


回答1:


It's just System.Array; it isn't generic. See here for a brief discussion: http://blogs.msdn.com/b/ericlippert/archive/2007/10/17/covariance-and-contravariance-in-c-part-two-array-covariance.aspx




回答2:


Arrays are older that generics. Arrays are available since .net 1, and generics were only introduced in .net 2. So they are no syntax sugar on top of generics.

Instead they derive from Array and use a lot of runtime magic to work.

In addition arrays of a reference type support some variance patterns not supported by generics. If I recall correctly this was done for Java compatibility.




回答3:


Far as I know, there wouldn't be an open generic type of Array; they existed before generics did in the .NET Framework. Arrays are just "weird"; the System.Array type exists mainly to provide O-O functionality to array objects. Only the runtime can derive from it.

HOWEVER, System.Array implements the generic IList<T>, which enforces the indexer. If you want an abstract that is strongly typed and will accept a strongly-typed array, use IList.




回答4:


It's not just syntactic sugar, it's an actual derivation of the System.Array class.



来源:https://stackoverflow.com/questions/5199943/what-is-the-open-generic-type-of-the-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!