473,395 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Array.Copy and Cloning an array don't seem to work differently

I would like to have an array of objects (whose class I define) and
then just invoke either:
MyClass [] clonedArray = (MyClass[]) myArray.Clone();
OR
Array.Copy(myArray, clonedArray, myArray.Length);

and have an array of cloned objects (i.e. the new array of objects
aren't the objects contained in my original array). But instead it
seems necessary for me to have to iterate over my entire array and
individually clone each element in the array. I thought at least
Array.Copy would do this for me. Does anyone know why it doesn't or if
I'm missing something that would make this idea work? Or do I actually
have to manually iterate over my array to achieve this?

Here is the test code so you can see what I mean:

class TestPair : ICloneable
{
public string a;
public string b;

public TestPair (string aParam, string bParam)
{
a = aParam;
b = bParam;
}

public object Clone()
{
return new TestPair(a, b);
}
}

public static void TestCloneOfClassWithPair()
{
TestPair testCloneOfPair1 = new TestPair("blah", "meh");

TestPair [] testPairs = new TestPair[1];
testPairs[0] = testCloneOfPair1;

// the following tries to use the Clone method of the array
//TestPair [] testCloneOfPairs = (TestPair [])testPairs.Clone();
// the following tries using Array.Clone
/*TestPair [] testCloneOfPairs = new TestPair[testPairs.Length];
Array.Copy(testPairs, testCloneOfPairs, testCloneOfPairs.Length);*/
// Only the following actually succeeds in giving me an array of
elements that don't reference the original array
TestPair [] testCloneOfPairs = new TestPair[testPairs.Length];
for (int i = 0;i < testPairs.Length;i++)
{
testCloneOfPairs[i] = (TestPair)testPairs[i].Clone();
}

if (testPairs[0] == testCloneOfPairs[0])
Trace.WriteLine("same object is referenced");

testCloneOfPairs[0].a = "new value";
Trace.WriteLine("testPairs - a: "+testPairs[0].a+" b:
"+testPairs[0].b);
}

Suggestions of why neither of the two other techniques succeed?

Thanks,
Novice

Aug 28 '06 #1
1 2029

il***********@gmail.com wrote:
I would like to have an array of objects (whose class I define) and
then just invoke either:
MyClass [] clonedArray = (MyClass[]) myArray.Clone();
OR
Array.Copy(myArray, clonedArray, myArray.Length);

and have an array of cloned objects (i.e. the new array of objects
aren't the objects contained in my original array). But instead it
seems necessary for me to have to iterate over my entire array and
individually clone each element in the array. I thought at least
Array.Copy would do this for me. Does anyone know why it doesn't or if
I'm missing something that would make this idea work? Or do I actually
have to manually iterate over my array to achieve this?

Here is the test code so you can see what I mean:

class TestPair : ICloneable
{
public string a;
public string b;

public TestPair (string aParam, string bParam)
{
a = aParam;
b = bParam;
}

public object Clone()
{
return new TestPair(a, b);
}
}

public static void TestCloneOfClassWithPair()
{
TestPair testCloneOfPair1 = new TestPair("blah", "meh");

TestPair [] testPairs = new TestPair[1];
testPairs[0] = testCloneOfPair1;

// the following tries to use the Clone method of the array
//TestPair [] testCloneOfPairs = (TestPair [])testPairs.Clone();
// the following tries using Array.Clone
/*TestPair [] testCloneOfPairs = new TestPair[testPairs.Length];
Array.Copy(testPairs, testCloneOfPairs, testCloneOfPairs.Length);*/
// Only the following actually succeeds in giving me an array of
elements that don't reference the original array
TestPair [] testCloneOfPairs = new TestPair[testPairs.Length];
for (int i = 0;i < testPairs.Length;i++)
{
testCloneOfPairs[i] = (TestPair)testPairs[i].Clone();
}

if (testPairs[0] == testCloneOfPairs[0])
Trace.WriteLine("same object is referenced");

testCloneOfPairs[0].a = "new value";
Trace.WriteLine("testPairs - a: "+testPairs[0].a+" b:
"+testPairs[0].b);
}

Suggestions of why neither of the two other techniques succeed?
By definition, default copies / clones are all shallow. You want a deep
copy.

In order to achieve this you have to either:

a) Define a static method in your TestPair class that clones an array:

public static TestPair[] CloneArray(TestPair[])
{
...
}

and then TestPair[] clone = TestPair.CloneArray(original);

or b) Define a whole new class that is a collection of TestPairs and
write a Clone method for it:

public class TestPairCollection ...
{
public object Clone()
{
...
}
}

Aug 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
13
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.