|
-
May 19th, 2008, 07:01 AM
#1
Dictionary Values copy
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?
Code:
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
-
May 19th, 2008, 07:24 AM
#2
Re: Dictionary Values copy
Code:
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.
Code:
public Foo(int i)
{
abc = i;
Console.WriteLine("Constructs Foo({0})", i);
}
-
May 19th, 2008, 07:28 AM
#3
Re: Dictionary Values copy
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?
 Originally Posted by cilu
Code:
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.
Code:
public Foo(int i)
{
abc = i;
Console.WriteLine("Constructs Foo({0})", i);
}
regards,
George
-
May 19th, 2008, 07:30 AM
#4
Re: Dictionary Values copy
You know, most of the time, the best way is to try it.
-
May 19th, 2008, 07:40 AM
#5
Re: Dictionary Values copy
Thanks cilu!
I have tested CopyTo will only copy the reference, not the references, to my surprise. :-)
Code:
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;
}
 Originally Posted by cilu
You know, most of the time, the best way is to try it.
regards,
George
-
May 19th, 2008, 09:32 AM
#6
Re: Dictionary Values copy
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.
Useful or not? Rate my posting. Thanks.
-
May 19th, 2008, 02:30 PM
#7
Re: Dictionary Values copy
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|