我正在处理涉及C#中数组的数据,当我使用foreach循环时,它给了我一条消息
无法将char类型转换为字符串
int[,] tel = new int[4, 8];
tel[0, 0] = 398;
tel[0, 1] = 3333;
tel[0, 2] = 2883;
tel[0, 3] = 17698;
tel[1, 0] = 1762;
tel[1, 1] = 176925;
tel[1, 2] = 398722;
tel[2, 0] = 38870;
tel[3, 1] = 30439;
foreach (string t in tel.ToString())
{
Console.WriteLine(tel +" " +"is calling");
Console.ReadKey();
}
这是因为当您foreach
将string
每个值都设置为a时char
,但是您正在尝试将其强制转换为string
。
foreach(string t in tel.ToString())
但是您不太可能希望继续foreach
,tel.ToString()
因为会返回tel
(System.Int32[,]
)类型的名称。相反,您可能想迭代所有值tel
for(int i=0; i<4; i++)
{
for(int j=0; j<8; j++)
{
Console.WriteLine(tel[i,j] +" is calling");
Console.ReadKey();
}
}
或者
foreach(int t in tel)
{
Console.WriteLine(t +" is calling");
Console.ReadKey();
}
请注意,某些值将为零,因为您未将值分配给tel
数组中的所有位置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句