在C#数组中添加和替换元素

在C#数组中添加和替换元素,c#,arrays,multidimensional-array,C#,Arrays,Multidimensional Array,我必须用C#中的一些数据进行操作。我的想法是将它们添加到数组中。该数组的元素将是(它将是包含3个元素的数组) 然后,我需要更改,即元素15应更改为 15, test21, comment22A, comment23 你能帮助我如何使用这种数组吗 提前谢谢你 目前还不清楚数组中的元素到底是什么,但看起来确实有一定的结构。我鼓励您将该结构封装在一个新类型中,这样您可能会得到一个: FooBar[] values = ...; values[15] = new FooBar(15, "test21"

我必须用C#中的一些数据进行操作。我的想法是将它们添加到数组中。该数组的元素将是(它将是包含3个元素的数组)

然后,我需要更改,即元素15应更改为

15, test21, comment22A, comment23
你能帮助我如何使用这种数组吗


提前谢谢你

目前还不清楚数组中的元素到底是什么,但看起来确实有一定的结构。我鼓励您将该结构封装在一个新类型中,这样您可能会得到一个:

FooBar[] values = ...;
values[15] = new FooBar(15, "test21", "comment22A", "comment23");
或者也可能类似,但有一个
列表
。多维数组(或者数组的数组)通常比一些封装良好的类型的单个集合更难使用。此外,你至少应该考虑使用比数组更高级别的抽象——参见Eric Lippert的博客文章以获得更多的细节。
如果您的第一个值是某种标识符,您甚至可能希望将其更改为
字典
关键字集合

不清楚数组中的哪些元素真正困难,但看起来确实有一定的结构。我鼓励您将该结构封装在一个新类型中,这样您可能会得到一个:

FooBar[] values = ...;
values[15] = new FooBar(15, "test21", "comment22A", "comment23");
或者也可能类似,但有一个
列表
。多维数组(或者数组的数组)通常比一些封装良好的类型的单个集合更难使用。此外,你至少应该考虑使用比数组更高级别的抽象——参见Eric Lippert的博客文章以获得更多的细节。
如果您的第一个值是某种标识符,您甚至可能希望将其更改为
字典
KeyedCollection

同意Jon Skeet的观点,下面是一个简单的实现

class Program
{
   class MyArrayType
   {
     public int MyInt { get; set; }
     public string Test { get; set; }
     public string Comment1 { get; set; }
     public string Comment2 { get; set; }
     public string Comment3 { get; set; }

   }

  static void Main()
  {

    List<MyArrayType> list = new List<MyArrayType>();
    list.Add(new MyArrayType { MyInt = 1, Test = "test1", Comment1 = "Comment1", Comment2 = "Comment3", Comment3 = "Comment3" });
    // so on

    list[15].MyInt = 15;
    list[15].Comment1 = "Comment";
    // so on
  }

}
类程序
{
类MyArrayType
{
公共int MyInt{get;set;}
公共字符串测试{get;set;}
公共字符串Comment1{get;set;}
公共字符串Comment2{get;set;}
公共字符串Comment3{get;set;}
}
静态void Main()
{
列表=新列表();
添加(新的MyArrayType{MyInt=1,Test=“test1”,Comment1=“Comment1”,Comment2=“Comment3”,Comment3=“Comment3”});
//诸如此类
列表[15].MyInt=15;
列表[15]。Comment1=“Comment”;
//诸如此类
}
}

同意Jon Skeet的观点,下面是一个简单的实现

class Program
{
   class MyArrayType
   {
     public int MyInt { get; set; }
     public string Test { get; set; }
     public string Comment1 { get; set; }
     public string Comment2 { get; set; }
     public string Comment3 { get; set; }

   }

  static void Main()
  {

    List<MyArrayType> list = new List<MyArrayType>();
    list.Add(new MyArrayType { MyInt = 1, Test = "test1", Comment1 = "Comment1", Comment2 = "Comment3", Comment3 = "Comment3" });
    // so on

    list[15].MyInt = 15;
    list[15].Comment1 = "Comment";
    // so on
  }

}
类程序
{
类MyArrayType
{
公共int MyInt{get;set;}
公共字符串测试{get;set;}
公共字符串Comment1{get;set;}
公共字符串Comment2{get;set;}
公共字符串Comment3{get;set;}
}
静态void Main()
{
列表=新列表();
添加(新的MyArrayType{MyInt=1,Test=“test1”,Comment1=“Comment1”,Comment2=“Comment3”,Comment3=“Comment3”});
//诸如此类
列表[15].MyInt=15;
列表[15]。Comment1=“Comment”;
//诸如此类
}
}

我完全同意Jon Skeet回答中的建议。在C#超过七年的专业发展中,我不记得曾经不止一次或两次使用多维数组。对于我们在旧语言中使用多维数组的大多数情况,该语言和.NET框架提供了更好的选择

也就是说,以下是如何为现有数组赋值

首先,由于您的问题没有指定数据类型,因此假设您已将数组声明为多维字符串数组:

string foo[,] = new string[42, 3];
可以这样访问第15行的第二列:

foo[15,2] = "comment22A";

你可以在C#in中找到更多关于多维数组的信息。

我完全同意Jon Skeet回答中的建议。在C#超过七年的专业发展中,我不记得曾经不止一次或两次使用多维数组。对于我们在旧语言中使用多维数组的大多数情况,该语言和.NET框架提供了更好的选择

也就是说,以下是如何为现有数组赋值

首先,由于您的问题没有指定数据类型,因此假设您已将数组声明为多维字符串数组:

string foo[,] = new string[42, 3];
可以这样访问第15行的第二列:

foo[15,2] = "comment22A";

你可以在C#in中找到更多关于多维数组的信息。

hmmm,你能更具体一点吗?我从未使用过那种数组。@user198003:什么类型的数组?我建议您使用自己的类型(我使用了
FooBar
作为一种占位符名称)来封装单个条目的所有数据。当你没有明确你想要达到的目标时,很难再明确了。嗯,你能更明确一点吗?我从未使用过那种数组。@user198003:什么类型的数组?我建议您使用自己的类型(我使用了
FooBar
作为一种占位符名称)来封装单个条目的所有数据。当你对自己想要达到的目标不明确时,很难再明确了。我喜欢这样。您能告诉我如何列出列表中的所有元素吗?foreach(列表中的MyArrayType t)抱歉,我在手机上,无法编写完整的语法foreach(列表中的MyArrayType t){int i=t.MyInt;//依此类推}我喜欢这样。您能告诉我如何列出列表中的所有元素吗?foreach(列表中的MyArrayType t)抱歉,我在手机上,无法编写完整的语法foreach(列表中的MyArrayType t){int i=t.MyInt;//so on}TRUE,+1用于分享经验;)正确,分享经验+1;)