How to add an array to another

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I have a array like:
string[] as1={ "Tigher", "Dog", ", "Lion"};
Now I want to make another array as2, which include all items in as1 and add
a new item "Wheel".
How can I make as2 with code in run time?
 
You might want to take a look at Array.Copy()

I have a array like:
string[] as1={ "Tigher", "Dog", ", "Lion"};
Now I want to make another array as2, which include all items in as1 and add
a new item "Wheel".
How can I make as2 with code in run time?
 
ad said:
I have a array like:
string[] as1={ "Tigher", "Dog", ", "Lion"};
Now I want to make another array as2, which include all items in as1 and add
a new item "Wheel".
How can I make as2 with code in run time?

-To it the direct way:

using System;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string[] as1={ "Tiger", "Dog", "", "Lion"};

string[] as2 = new string[as1.Length + 1];
as1.CopyTo(as2, 0);
as2[as2.Length - 1] = "Wheel";

foreach (string s in as1)
Console.WriteLine(s);
}
}
}

-Use a collection:

using System;
using System.Collections.Generic;

namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
ICollection<string> as1 = new List<string>();
as1.Add("Tiger");
as1.Add("Dog");
as1.Add("");
as1.Add("Lion");

as1.Add("Wheel");

foreach (string s in as1)
Console.WriteLine(s);
}
}
}

Or use a non generic collection out of the System.Collections namespace.

HTH,
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top