foreach 遍历ArrayList 无需拆箱操作

dingdot 2011-11-09 11:19:45
在集合ArrayList中,添加的元素都会自动的转换为Object类型。
Ojbect是不能隐式的转换为其它类型的,必须经过强制转换

ArrayList intArray=new ArrayList();
int a=1;
int b=2;
int c=3;
intArray.Add(a);
intArray.Add(b);
intArray.Add(c);

//遍历intArray
foreach(int num in intArray)
{
Console.WriteLine((num+1).ToString());
}

这个遍历是可以正常执行的,但是有个问题就是
intArray中的元素不都是Object类型么? 它并没有强制转成int类型啊,难道在这里
foreach会强制的将Object类型转为int?

...全文
356 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyonghong159632 2011-11-09
  • 打赏
  • 举报
回复
这都让楼主发现了....
GodDices 2011-11-09
  • 打赏
  • 举报
回复
装箱拆箱什么的。。首先自己要知道类型。。。你自己都不知道,计算机怎么会知道。。。
Flashcom 2011-11-09
  • 打赏
  • 举报
回复
如果foreach (int num in intArray),不是强制转换那么你用
foreach (object num in intArray)
后面就不可以num+1了
Flashcom 2011-11-09
  • 打赏
  • 举报
回复
foreach (int num in intArray)
你这句不是进行的转换了吗,强制把num定义为int类型了
gatx166 2011-11-09
  • 打赏
  • 举报
回复
用List<int> 代替比较好 DataRow 的row是Object的
黄亮 2011-11-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 orangeevan 的回复:]
C# code


private static void test()
{
ArrayList intArray = new ArrayList();
int a = 1;
int b = 2;
string c = "a";
……
[/Quote]
这种隐式转换要求类型一致
黄亮 2011-11-09
  • 打赏
  • 举报
回复
拆箱操作隐式完成了
mabaolin 2011-11-09
  • 打赏
  • 举报
回复
IL_0044: unbox.any [mscorlib]System.Int32
OrangeEvan 2011-11-09
  • 打赏
  • 举报
回复

private static void test()
{
ArrayList intArray = new ArrayList();
int a = 1;
int b = 2;
string c = "a";
intArray.Add(a);
intArray.Add(b);
intArray.Add(c);

//遍历intArray
foreach (int num in intArray)
{
Console.WriteLine((num + 1).ToString());
}

}


“指定转换无效”。。。楼主自己可以试试!
mabaolin 2011-11-09
  • 打赏
  • 举报
回复
怎么没掉unbox?

IL_003b: br.s IL_0057
IL_003d: ldloc.s CS$5$0000
IL_003f: callvirt instance object [mscorlib]System.Collections.IEnumerator::get_Current()
IL_0044: unbox.any [mscorlib]System.Int32
IL_0049: stloc.s num
IL_004b: ldloca.s num
IL_004d: call instance string [mscorlib]System.Int32::ToString()
IL_0052: call void [mscorlib]System.Console::WriteLine(string)
IL_0057: ldloc.s CS$5$0000
IL_0059: callvirt instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()
IL_005e: brtrue.s IL_003d
IL_0060: leave.s IL_0077
dingdot 2011-11-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kuki4552993 的回复:]
这是因为你foreach里指定了将ArrayList指定为int数据,内部就会尝试将数据一个个地转化成int,但是有可能就会转化失败,你不信可以往里面放一些不能转成int的数据,就会有异常。
[/Quote]
那就是说在foreach中,会自动的将集合中的object类型强制的转换为你需要的类型,是吧?
幸福小6 2011-11-09
  • 打赏
  • 举报
回复
这是因为你foreach里指定了将ArrayList指定为int数据,内部就会尝试将数据一个个地转化成int,但是有可能就会转化失败,你不信可以往里面放一些不能转成int的数据,就会有异常。
dingdot 2011-11-09
  • 打赏
  • 举报
回复
我当然知道可以用泛型,但是我想知道这里为什么无需拆箱呢?
Flashcom 2011-11-09
  • 打赏
  • 举报
回复
用List<T>类型代替ArrayList
tianzhengning 2011-11-09
  • 打赏
  • 举报
回复
大家怎么都没看到6楼的回复呢?
幸福小6 2011-11-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dingdot 的回复:]

引用 3 楼 kuki4552993 的回复:
这是因为你foreach里指定了将ArrayList指定为int数据,内部就会尝试将数据一个个地转化成int,但是有可能就会转化失败,你不信可以往里面放一些不能转成int的数据,就会有异常。

那就是说在foreach中,会自动的将集合中的object类型强制的转换为你需要的类型,是吧?
[/Quote]
是的,自动强制转换了。

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧