2012-07-24 88 views
0

我有一个数组列表错误的ArrayList在C#2008

private ArrayList rps = new ArrayList(); 

现在下面的代码是工作在2005年很好,但不是在Visual Studio 2008中

int min = Convert.ToInt32(rps.Item(0)); 
int max = Convert.ToInt32(rps.Item(rps.Count - 1)); 

Error: System.Collections.ArrayList does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'System.Collections.ArrayList' could be found (are you missing a using directive or an assembly reference?`

回答

3

该代码在VS 2005中也不起作用。类似的东西可能在VB中起作用,但在C#中不起作用。 C#代码是:

int min = Convert.ToInt32(rps[0]); 
int max = Convert.ToInt32(rps.Item[rps.Count - 1]); 

不过,我建议你开始使用泛型集合比如List<T>代替。

+0

true,我把它从VB转换成 – Zerotoinfinity 2012-07-24 20:51:01

+0

是的,当我看到代码的第一件事就是想知道为什么VB.NET被标记为C#...直到我看到';'。 – Oded 2012-07-24 20:52:17

3

使用indexer

int min = Convert.ToInt32(rps[0]); 

还要考虑使用的List<T>代替ArrayList

2

你有一个语法错误:

rps.Item(0) 

应该是:

rps[0] 

注 - 你真的不应该使用ArrayList - 它早于仿制药这是什么,你应该使用是代替。