-1

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:

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

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

anyone can help me?

user3015248
  • 89
  • 1
  • 9

8 Answers8

3

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

Steve's a D
  • 3,801
  • 10
  • 39
  • 60
2

Arrays don't play well with this idea. Usually, people use List for this kind of thing.

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

names.Add("Steve");

foreach (string i in names)
{
    richTextBox1.AppendText(i + Environment.NewLine);
}
azurelogic
  • 801
  • 6
  • 12
2

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. 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). Now you can do:

    List<string> names = new List<string> {"Matt", "Joanne", "Robert"};
    names.Add("Steve");
    names.Add("Another one");
    
Nikola Dimitroff
  • 6,127
  • 2
  • 25
  • 31
1

Although you can expand .NET arrays, in a situation like this you would be better off with a List<string>:

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

Now you can add a new name to names by calling Add:

names.Add("Steve");

Note: rather than using AppendText in a loop, you could use string.Join, like this:

richTextBox1.AppendText(names.Join(Environment.NewLine, names));
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

To add the Item to the array, using the code you provided, you can do this:

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<string> names = new List<string> { "Matt", "Joanne", "Robert" };
names.Add("Steve");     // Add a new entry
richTextBox1.AppendText(String.Join(Environment.NewLine, names));
Tony
  • 16,527
  • 15
  • 80
  • 134
0

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";
0

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!

PaulP
  • 11
  • 4
0

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.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62