不能隐式地将类型'int []'转换为'object []'? [英] Cannot implicitly convert type 'int[]' to 'object[]'?
本文介绍了不能隐式地将类型'int []'转换为'object []'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
根据我的功能,我想将int []转换为object []。我试图这样做,但我不能转换它。
示例:
int [] intrs = new int [] {< span class =code-digit> 400 , 435 , 446 , 900 };
object [] obj = intrs;
你能告诉我如何将int []转换为object []吗?
提前感谢..
解决方案
这里有一个非常好的解释,你应该阅读 - 无法将值类型数组转换为params对象[] [ ^ ]
int [] intrs = new int [] { 400 , 435 , 446 , 900 };
object [] obj =( from i in intrs 选择 i).Cast< object>()。ToArray();
它会帮助你使用这种投射方法
你不能简单地转换一个数组 - 你不能明确地投了它。你需要做的是从现有数据创建一个新的对象[]
。
尽管如此:
int [] intrs = new int [] { 400 , 435 , 446 , 900 };
object [] obj = Array.ConvertAll(intrs,x = > (对象跨度>)x)的;
Hi all,
Based on my functionality I want convert int[] to object[]. I tried to do this but I can't to convert this.
example:
int[] intrs = new int[] { 400, 435, 446, 900 };
object[] obj = intrs;
Can you any one tell me how to convert int[] to object[]?
thanks in advance..
解决方案
There is a very nice explanation for the issue here that you should read - Can't convert value type array to params object[][^]
int[] intrs = new int[] { 400, 435, 446, 900 }; object[] obj = (from i in intrs select i).Cast<object>().ToArray();
it will help you use this casting method
You can't convert an array that simply - you can't explicitly cast it either. What you have to do is create a newobject[]
from the existing data.
It's pretty easy though:
int[] intrs = new int[] { 400, 435, 446, 900 }; object[] obj = Array.ConvertAll(intrs, x => (object)x);
这篇关于不能隐式地将类型'int []'转换为'object []'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文