AKrishna
AKrishna

Reputation: 11

c # - copying elements of one array to another - compile error

Learning some basics.. I'm trying to copy elements of an array to another. Let's say I don't know the size of the array 'bar'. So, I create an empty array 'arr' to copy the elements to bar into. The code below doesn't work.

It works if I replace

string[] arr ={} to string[] arr ={"",""}

How to declare an empty array and what should I modify in my code to achieve my goal?

Thanks!

//code

string[] bar =  {"test", "user"};
string[] arr =  {};



//iterate from the first to the last element of array bar
for (int i =0;i<bar.Length-1;i++)

{

  Console.WriteLine("copy");

  //copy string from bar to arr
  arr[i]= bar[i];

  //display the copied content from new array
  Console.WriteLine(arr[i]);  


}

Upvotes: 1

Views: 237

Answers (5)

dwbartz
dwbartz

Reputation: 886

If you're going to use arrays, you'll want to create the second array as the same size as the first.

string[] bar =  {"test", "user"};
string[] arr =  new string[bar.Length];

If you know ahead of time that your array will be two, then you can just create it to be size two. Otherwise you'll want to inspect the size of the array you're copying from. If you know that you'll be adding and/or removing items, you'll want to use a different collection.

Upvotes: 0

Qiong
Qiong

Reputation: 61

You can modify the line as:

string[] arr = new string[4];

or

List<string> arr = new List<string>();

Upvotes: 0

Jonesopolis
Jonesopolis

Reputation: 25370

in C#, arrays are of a fixed size. So when you create your array with size 0, you can't change the number of items it will contain, without re-instantiating it.

If you want to use a collection you can actively add/remove from (as is very common), consider using a List<T>:

string[] bar =  {"test", "user"};
List<string> list = new List<string>();

for (int i =0;i<bar.Length-1;i++)
{
  list.Add(bar[i]);
  Console.WriteLine(list[i]);  
}

Upvotes: 2

Marshall Tigerus
Marshall Tigerus

Reputation: 3764

Arrays are fixed in size, which is why things like Lists are preferred over them.

In the case where you change your array definition to: string[] arr ={"",""} you are defining an array with a size of 2, same as your original array. When you try to copy it, the compiler already has everything allocated and ready to go, so it knows where position 0 and position 1 are in the array arr.

In the example in your code, where you have the array defined by string[] arr = {}; you are giving it an empty array (array size 0). The compiler has an issue, because it cannot reference position 0 or position 1 on an array that is empty.

Upvotes: 0

hazevich
hazevich

Reputation: 468

By string[] arr = {}; you're instantiating an empty array of ZERO length, thus you need to define it like string[] arr = string[bar.Length];.

UPD: Your code worked with string[] arr = {"",""}, because in this case you defined an array of length 2 using a two empty strings, but that's a code smell.

Upvotes: 0

Related Questions