如何声明一个字节ArrayList

时间:2016-03-31 12:44:22

标签: c# arraylist byte

我正在尝试

var mahByteArray = new ArrayList<byte>();

它不起作用。 它说:

  

非泛型类型'System.Collectios.ArrayList'不能与之一起使用   类型参数

声明字节ArrayList的正确方法是什么?

4 个答案:

答案 0 :(得分:3)

您将 Java ArrayList集合 C#List通用集合混淆。两者都用于声明集合,但第一个在Java中用作通用类List for Collections框架中定义的类型,最后一个在C#语言中用作隐式泛型类型。

因此,您必须声明为List类型。详见List

var mahByteArray = new List<byte>();

List<byte> mahByteArray = new List<byte>() { 2, 3, 4 };

答案 1 :(得分:3)

确定您可以使用ArrayList

var mahByteArray = new ArrayList();
mahByteArray.Add((byte) 230);

答案 2 :(得分:2)

ArrayList<>不是通用的。您可以使用通用List<>代替

var mahByteArray = new List<byte>();

答案 3 :(得分:1)

ArrayList不是通用的。请改用System.Collections.Generic.List<T>List<T>类是ArrayList类的通用等价物。它使用一个数组实现IList<T>通用接口,该数组的大小根据需要动态增加。

var mahByteArray = new List<byte>();

另请看一下:Difference between ArrayList and Generic List.