本文介绍了不能char类型转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做的,涉及阵列在C#中的数据,当我使用foreach循环它给了我一个消息
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();
}
解决方案
That is becuase when you foreach
over a string
each value will be a char
, but you are trying to cast them to string
.
foreach(string t in tel.ToString())
But it's unlikely that you want to foreach
on tel.ToString()
as the will return the name of the type of tel
(System.Int32[,]
). Instead you probably want to iterate all the values in tel
for(int i=0; i<4; i++)
{
for(int j=0; j<8; j++)
{
Console.WriteLine(tel[i,j] +" is calling");
Console.ReadKey();
}
}
Or
foreach(int t in tel)
{
Console.WriteLine(t +" is calling");
Console.ReadKey();
}
Note that some of the values will be zero since you do not assign values to all the positions in the tel
array.
这篇关于不能char类型转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!