2
Answers

Dictionary Values copy

George George

George George

16y
2.4k
1

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;
    }
[/Code]


thanks in advance,
George

Answers (2)