你会如何转换object[,]成string[,]?
Object[,] myObjects= // sth
string[,] myString = // ?!? Array.ConvertAll(myObjects, s => (string)s) // this doesn't work
Run Code Online (Sandbox Code Playgroud)
任何建议赞赏.
编辑:当然,循环解决方案显然会做到这一点,但我在组织和性能方面都设想了更优雅的解决方案.
编辑2:object[,]包含当然strings(和数字,但这现在无关紧要).
Object[,] myObjects = new Object[3, 2] { { 1, 2 }, { 3, 4 },
{ 5, 6 } };
string[,] myString = new string[3, 2];
for (int i = myObjects.GetLowerBound(0); i < myObjects.GetUpperBound(0); i++)
{
for (int j = myObjects.GetLowerBound(1); j < myObjects.GetUpperBound(1); j++)
{
myString[i, j] = myObjects[i, j].ToString();
}
}
foreach (var item in myString)
{
Console.WriteLine("{0} - {1}", item.GetType(), item);
}
Run Code Online (Sandbox Code Playgroud)
输出将是;
System.String - 1
System.String - 2
System.String - 3
System.String - 4
System.String - 5
System.String - 6
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
627 次 |
| 最近记录: |