Click to See Complete Forum and Search --> : Dictionary Values copy


George2
May 19th, 2008, 07:01 AM
Hello everyone,


Here is my code to make a duplicate value copy of all the Values in a dictionary instance -- not just reference. So, I use CopyTo method.

My current issue is, I think my code is stupid but do not know how to solve it. My specific concern is,

1.

the code Foo[] FooArray = new Foo[3] creates 3 new instances of Foo, the code dic.Values.CopyTo(FooArray, 0) will overwrite the 3 new instances of Foo? So, seems it is stupid to create 3 new instances of Foo in Foo[] FooArray = new Foo[3], but never use them and overwrite them? Any ideas? Is it an issue?

2.

Maybe I am wrong, the code Foo[] FooArray = new Foo[3] creates only references variable of Foo class? Not class instances variable themselves?


class Foo
{
public int abc;

public Foo (int i)
{
abc = i;
}

}

public static void Main()
{

Dictionary<int, Foo> dic = new Dictionary<int,Foo>();
dic.Add(1, new Foo(10));
dic.Add(1, new Foo(20));
dic.Add(3, new Foo(30));

Foo[] FooArray = new Foo[3];

dic.Values.CopyTo(FooArray, 0);

return;
}



thanks in advance,
George

cilu
May 19th, 2008, 07:24 AM
Foo[] FooArray = new Foo[3];

That does not create 3 Foo instances. It only creates an array that has 3 elements set to null. Have you tried to run it in debugger and look at the values?

You could also put a message in the constructor of Foo and see that it's not printed.

public Foo(int i)
{
abc = i;
Console.WriteLine("Constructs Foo({0})", i);
}

George2
May 19th, 2008, 07:28 AM
Thanks cilu,


So this code Foo[] FooArray = new Foo[3] will only create 3 references and make them refer to NULL? Then code dic.Values.CopyTo(FooArray, 0) will create 3 instances of Foo class, and assign the 3 NULL references variables in FooArray to point to the instances?


Foo[] FooArray = new Foo[3];

That does not create 3 Foo instances. It only creates an array that has 3 elements set to null. Have you tried to run it in debugger and look at the values?

You could also put a message in the constructor of Foo and see that it's not printed.

public Foo(int i)
{
abc = i;
Console.WriteLine("Constructs Foo({0})", i);
}



regards,
George

cilu
May 19th, 2008, 07:30 AM
You know, most of the time, the best way is to try it.

George2
May 19th, 2008, 07:40 AM
Thanks cilu!


I have tested CopyTo will only copy the reference, not the references, to my surprise. :-)


class Foo
{
public int abc;

public Foo (int i)
{
abc = i;
}

}

public static void Main()
{

Dictionary<int, Foo> dic = new Dictionary<int,Foo>();
dic.Add(1, new Foo(10));
dic.Add(2, new Foo(20));
dic.Add(3, new Foo(30));

Foo[] FooArray = new Foo[3];

dic.Values.CopyTo(FooArray, 0);

// change the 1st one
FooArray[0].abc = 40;

// output 40
Console.WriteLine(dic[1].abc);

return;
}


You know, most of the time, the best way is to try it.


regards,
George

torrud
May 19th, 2008, 09:32 AM
C# works in most cases only with references. For getting duplicates you should implement ICloneable in your business class and call the clone() method for getting a second instance of the object. Thats the usual way in C# as far as I know.

cilu
May 19th, 2008, 02:30 PM
I have tested CopyTo will only copy the reference, not the references, to my surprise. :-)
Hm, Foo is a class right? That makes it a reference type. So of course it behaves as you observed. In .NET there are two kind of types: value types, copied/passed by value and reference types, copied/passed by reference.

Make Foo a struct and then see what happens.