执行以下代码时运行时异常 [英] Run Time Exception While Executing Following Code
本文介绍了执行以下代码时运行时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
执行以下代码时的运行时异常为什么?
Run Time Exception While Executing Following Code Why ?
// Declare and set array element values
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
IEnumerable<string> a = array2.Cast<string>().ToArray();
推荐答案
因为您无法将int转换为字符串 - 你正在做的是相当于这样做:
Because you can't Cast a int to a string - what you are doing is the equivalent of doing this:
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
IEnumerable<string> a = new List<string>();
foreach (int i in array2)
{
a.Add((string) i));
}
哪个不行。
试试这个:
Which will not work.
Try this:
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
IEnumerable<string> a = array2.Select(i => i.ToString());
这篇关于执行以下代码时运行时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文