Search code examples
c#equalsarrays

How to make arrays values equal in C#?


Let's say that I have 2 string arrays with different values:
string[] sArray1 = new string[3]{"a","b","c"};
string[] sArray2 = new string[3]{"e","f","g"}

And I want to make values of sArray1 equal to values of sArray2 (I know I can write it like this) : sArray1[0] = sArray2[0]; sArray1[1]= sArray2[1]; sArray1[2]=sArray2[2];

For 3 values it's easy, but what if I had 100 values in an array? Is there any other way that I can make array values equal?

p.s. sorry for my bad English :(


Solution

  • Something like this (with a little error checking):

            if (sArray2.Length == sArray1.Length)
            {
                sArray2.CopyTo(sArray1, 0);    
            }
    

    Regards