简体   繁体   English

在C#中为数组添加值

[英]Add value to array in C#

Hello I'm just starting in c # and am practicing with arrays, my question is how I can add a name called "steve" the array of this code: 您好,我只是从C#开始,并且正在练习数组,我的问题是如何添加一个名为“ steve”的代码的数组:

    string[] names = new string[] {"Matt", "Joanne", "Robert"};

    foreach (string i in names)
    {
        richTextBox1.AppendText(i + Environment.NewLine);
    }

anyone can help me? 有人可以帮助我吗?

You can resize an array , however its probably better to just use a list if you need a collection who's size changes. 您可以调整数组的大小 ,但是如果您需要更改大小的集合,则最好只使用列表。

Note that resizing an array actually just creates a new array of the size you want behind the scenes and copies over all the data 请注意,调整数组大小实际上只是在后台创建一个您想要的大小的新数组,然后复制所有数据

Arrays don't play well with this idea. 数组不能很好地满足这个想法。 Usually, people use List for this kind of thing. 通常,人们将List用于此类事情。

List<string> names = new List<string> {"Matt", "Joanne", "Robert"};

names.Add("Steve");

foreach (string i in names)
{
    richTextBox1.AppendText(i + Environment.NewLine);
}

You can't add elements to an array once the array has been created. 创建数组后,无法将元素添加到数组中。 You can: 您可以:

  • Add the element before the array has been created as a literal: 在将数组创建为文字之前添加元素:

     string[] names = new string[] {"Matt", "Joanne", "Robert", "Steve", "Another name", "Tons of other names"}; 
  • Or you can use a collection that allows you to add elements after it has been created such as a List . 或者,您可以使用一个集合,该集合允许您在创建元素后添加元素,例如List To use a List instead of array, make sure you have the following directive using System.Collections.Generic at the top of your main file (should be included by default). 要使用List而不是数组,请确保在主文件顶部using System.Collections.Generic以下指令(默认情况下应包括在内)。 Now you can do: 现在您可以执行以下操作:

     List<string> names = new List<string> {"Matt", "Joanne", "Robert"}; names.Add("Steve"); names.Add("Another one"); 

Although you can expand .NET arrays , in a situation like this you would be better off with a List<string> : 尽管可以扩展.NET数组 ,但在这种情况下,最好使用List<string>

List<string> names = new List<string> {"Matt", "Joanne", "Robert"};

Now you can add a new name to names by calling Add : 现在,您可以通过调用Addnames添加新名称:

names.Add("Steve");

Note: rather than using AppendText in a loop, you could use string.Join , like this: 注:而不是使用AppendText在一个循环中,你可以使用string.Join ,就像这样:

richTextBox1.AppendText(names.Join(Environment.NewLine, names));

To add the Item to the array, using the code you provided, you can do this: 要使用提供的代码将Item添加到数组,可以执行以下操作:

string[] names = new string[] { "Matt", "Joanne", "Robert" };

Array.Resize(ref names, names.Length + 1);
names[names.Length - 1] = "Steve";

foreach (string i in names)
{
    richTextBox1.AppendText(i + Environment.NewLine);
}

Consider using this code instead, that uses List: 考虑使用以下代码,该代码使用List:

List<string> names = new List<string> { "Matt", "Joanne", "Robert" };
names.Add("Steve");     // Add a new entry
richTextBox1.AppendText(String.Join(Environment.NewLine, names));

The array has a fix size. 阵列具有固定大小。 At first you've created that with three elements, so it will have three elements. 首先,您使用三个元素创建了该对象,因此它将包含三个元素。 You can modify any element so: 您可以修改任何元素,以便:

names[index] = "value";

You can make a list from an Array by writting: 您可以通过以下方式从数组中列出:

List<string> list = names.OfType<string>().ToList();

and then continue from there as the others mentioned! 然后像其他人一样从那里继续!

Example for resizing your array: 调整数组大小的示例:

string[] names = { "Matt", "Joanne", "Robert" };
Array.Resize(ref names, names.Length + 1);
names[names.Length - 1] = "Steve";

Steve has given the proper reference above. 史蒂夫在上面已经给出了适当的参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM