【问题标题】:Accessing an Object[] inside a List访问 List 中的 Object[]
【发布时间】:2015-03-14 23:03:50
【问题描述】:

我在访问 List 中的 Object[] 时遇到问题。

object[] datarow = null;
List<object> foo = new List<object>();
for (int i = 0; i < themonth.Length; i++) {    datarow = new object[]
{
   datarow = new object[] {themonth[i], aaa[i], bbb[i], ccc[i], ddd[i], eee[i], fff[i], ggg[i], hhh[i], iii[i] };
   foo.Add(datarow);
}

当我尝试输出“foo”列表的内容时,我只是得到 System.Object[] 作为输出。

for (int i = 0; i < foo.Count; i++)
{
   Console.WriteLine(foo.ElementAt(i));
}

调试器将 Object[] 显示为 Type - 扩展它 Object[] 包含放入 12 个数组的数据。 整个结果将是一个 10 列/12 行的列表/数组,我想以某种方式将其写回 .csv 文件。

任何想法如何实现这一目标?

我已经尝试创建一个新的字符串[],如下所示。

string[] test = ((IEnumerable)foo).Cast<object>().Select(x => x.ToString()).ToArray();

test[i] 的输出仍然是 System.Object[] 但没有数据。

【问题讨论】:

标签: c# arrays


【解决方案1】:

您认为ToString() 应该为objects 数组显示什么?

也许您想调用每个object 中的ToString()?类似(未经测试)

var test = string.Join("\n", foo.Select(x => string.Join(" ", x.Select(y => y.ToString())));

【讨论】:

    猜你喜欢
    • 2021-04-15
    • 2012-07-09
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2012-04-16
    相关资源
    最近更新 更多