0

From my understanding this line of Code

Array.Copy(intArray, intArray2, intArray.Length);

Both copies the elements of intArray into intArray2 and specifies the length of the Destination Array. However, It could also mean that intArray.Length as the last parameter specifies the range of indexes to copy to.

For example:

using System;
public class myClass{
    public static void Main(){

        int[] intArray = new int[] {1,2};
        int[] intArray2 = new int[intArray.Length + 1];
        Array.Copy(intArray, intArray2, intArray.Length);
        intArray = intArray2;
        foreach(int i in intArray){
            Console.Write(intArray[i]);
        }
    }
}

why does this print out 201?

15
  • 7
    C# != C++ != C - don't say "C based languages" - they behave differently. Commented Jul 23, 2013 at 14:29
  • programmers.stackexchange.com/questions/78846/… ... en.wikipedia.org/wiki/List_of_C-based_programming_languages Commented Jul 23, 2013 at 14:32
  • Those are all languages that have C style syntax - they are not based on C (C# is managed - no direct memory management, for instance). Commented Jul 23, 2013 at 14:33
  • and each of the C-based languages define arrays differently this is just a question about C# Commented Jul 23, 2013 at 14:34
  • Just because Memory direct memory management is not forced does not make it Non C-Based. If thats the case then you can Say Objective-C is not a C-Based language, since you can take turn on autoReferencing i.e Garabage collector. So yea I believe you are wrong. Commented Jul 23, 2013 at 14:35

1 Answer 1

3

It doesn't change the size of the array. Look at what you are doing.

This initialises a new array with a Length or 2. Creating the array

int[] intArray = new int[] {1,2};
int[0] = 1
int[1] = 2

This line creates a new array of length 3

int[] intArray2 = new int[intArray.Length + 1];

intArray.Length returns 2, then you add 1 to it. Making intArray2 3 in length.

By default all int array values are set to 0 in intArray2.

i.e.

intArray2[0] = 0 
intArray2[1] = 0 
intArray2[2] = 0

You Copy function is copying the values from intArray to intArray2 making

intArray2[0] = 1
intArray2[1] = 2 
intArray2[2] = 0

the line

intArray = intArray2 

doesn't copy the values back to intArray. It points intArray to the address of intArray2.

The way your foreach loop is written causes the array to be printed as:

intArray[1] = 2
intArray[2] = 0
intArray[0] = 1

It should be written as

foreach(int i in intArray)
{
    Console.Write(i);
}

Update

From my understanding this line of Code

Array.Copy(intArray, intArray2, intArray.Length);

Both copies the elements of intArray into intArray2 and specifies the length of the Destination Array. However, It could also mean that intArray.Length as the last parameter specifies the range of indexes to copy to.

In both these statements you are not quite correct.

The Array.Copy method parameters, in particular the last one you refer to, is the number of elements to copy from the origin array. It won't specify the length of the destination array. Your use of the word range, isn't quite correct either. That would be (IMHO)

Array.Copy(intArray, intArray2, startIndex, endIndex);

which I don't think exists in the .NET Framework.

Sign up to request clarification or add additional context in comments.

2 Comments

oh I didn't even see that index abuse there, that's why it prints 201 instead of 120... man that little snippet is a spectacular specimen
foreach(int i in intArray) { Console.Write(intArray); } This prints out System.Int32[]System.Int32[]System.Int32[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.