将值添加到来自不同类的C#数组

Joh*_*ohn 0 .net c# arrays

我正在尝试将用户在单独的窗体上输入的新值添加到以下数组中:

public class NameValue
  {
    public string Name;
    public string Value;
    public NameValue() { Name = null; Value = null; }
    public NameValue(string name, string value) { Name = name; Value = value; }
  }

   public class DefaultSettings
  {
    public static NameValue[] Sites = new NameValue[] 
    {
        new NameValue("los angeles, CA", "http://losangeles.craigslist.org/"),
    };

    public static NameValue[] Categories = new NameValue[] 
    {
        new NameValue("all for sale", "sss"),
    };
   }
Run Code Online (Sandbox Code Playgroud)

如何在保留旧数组值的同时将新值添加到数组中?

编辑

我尝试使用Noren先生的功能:

        static void AddValueToSites(NameValue newValue)
    {
        int size = DefaultSettings.Sites.Length;
        NameValue[] newSites = new NameValue[size + 1];
        Array.Copy(DefaultSettings.Sites, newSites, size);
        newSites[size] = newValue;
        DefaultSettings.Sites = newSites;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        NameValue newSite = new NameValue("Test, OR", "http://portland.craigslist.org/"); 
        AddValueToSites(newSite);
        Close();
    }
Run Code Online (Sandbox Code Playgroud)

但这不起作用......我从中获取数据的类是:

public partial class Location : Office2007Form
{
    public Location()
    {
        InitializeComponent();
    }
    static void AddValueToSites(NameValue newValue)...
    private void button1_Click(object sender, EventArgs e)...
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*ers 5

您永远无法更改数组的大小.你需要使用类似的东西List.

由于您使用的是名称/值对,因此应考虑使用Dictionary<TKey,TValue>.

最后,如果你想让不同的类贡献给数组的内容,那么这不会发生.