【问题标题】:data[,] into list<string> or list<object> in c#c# 中的 data[,] 到 list<string> 或 list<object>
【发布时间】:2012-06-14 15:00:39
【问题描述】:

我有一个二维数组object[,] data;。我想将对象的第一列转换为list&lt;string&gt; 或list&lt;object&gt;。这里有我的代码:

List<string> resultsFields = new List<string>();

object tmp = oRtx.get_ListFields(this.Instrument.ElementAt(i).Key.ToString(), RT_FieldRowView.RT_FRV_EXISTING, RT_FieldColumnView.RT_FCV_VALUE);
if (tmp != null)
{
    object[,] data = (object[,])Convert.ChangeType(tmp, typeof(object[,]));
    for (int j = 0; j < numItems; j++)
         resultsFields.Add(data[j, 0].ToString());
}

在这段代码中,我将data[j, 0] 上的每个项目添加到我的列表中。 我想知道是否有更快的方法将我的对象的第一列 ([0]) 转换为列表

【问题讨论】:

    标签: c# arrays list object casting


    【解决方案1】:

    我能看到的唯一性能改进是创建具有指定容量的列表:

    List<string> resultsFields = new List<string>(data.GetLength(0));
    

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      相关资源
      最近更新 更多