简体   繁体   English

C#中的数组与通用

[英]Arrays vs Generic in C#

I have noticed that array, in c#, implements ICollection<T> . 我注意到c#中的数组实现了ICollection<T> How can an array implement a generic container interface, yet not be generic itself? 数组如何实现通用容器接口,但本身不是通用的? Is it possible for us to do the same? 我们可以这样做吗?

Edit: I would also like to know how the array is not generic, yet it accepts any type and has type safety. 编辑:我也想知道数组是如何通用的,但它接受任何类型并具有类型安全性。

public class ListOfStrings : IList<string>
{
...
}

This is a great example that demonstrates that we can create non-generics from a generic (Thank you MarcinJuraszek!!). 这是一个很好的例子,表明我们可以从泛型创建非泛型(谢谢MarcinJuraszek !!)。 This collection would be stuck with strings. 这个集合会被字符串困住。 My guess is that it has nothing to do with the generic value type declaration of string and is some internal wiring that I am unfamiliar with. 我的猜测是它与字符串的泛型值类型声明无关,而且是我不熟悉的一些内部连线。

Thank you again! 再次感谢你!

Yes, it's totally possible. 是的,这完全有可能。 You can declare something like this: 你可以声明这样的东西:

public class MyListOfStrings : IList<string>
{
}

and as long as you implement all the properties/methods IList<string> requires you to everything will work just fine. 只要你实现了所有的属性/方法, IList<string>要求你一切都能正常工作。 As you can see MyListOfStrings is not generic. 如您所见, MyListOfStrings不是通用的。

You should also remember that Arrays are special types, and there is a bunch of stuff going on with them that's not happening with regular user-defined types. 你还应该记住,数组是特殊的类型,并且随着它们发生了许多事情,这些事情并没有发生在常规的用户定义类型中。 Some of it is described on MSDN , and the part that seem to be related to your questions is here: 其中一些在MSDN上有描述,而且似乎与您的问题相关的部分在这里:

Starting with the .NET Framework 2.0, the Array class implements the System.Collections.Generic.IList<T> , System.Collections.Generic.ICollection<T> , and System.Collections.Generic.IEnumerable<T> generic interfaces. 从.NET Framework 2.0开始, Array类实现System.Collections.Generic.IList<T>System.Collections.Generic.ICollection<T>System.Collections.Generic.IEnumerable<T>泛型接口。 The implementations are provided to arrays at run time, and as a result, the generic interfaces do not appear in the declaration syntax for the Array class. 这些实现在运行时提供给数组,因此,泛型接口不会出现在Array类的声明语法中。 In addition, 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 . 将数组转换为其中一个接口时要注意的关键是添加,插入或删除元素的成员会抛出NotSupportedException

As you can see Array implements IList<T> , ICollection<T> and IEnumerable<T> in a special way, and it's not something you can do with your own type. 正如您所看到的, Array以特殊方式实现了IList<T>ICollection<T>IEnumerable<T> ,而且您不能使用自己的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM