Click here to Skip to main content
16,011,471 members
Home / Discussions / C#
   

C#

 
GeneralRe: Dictionary Values copy Pin
George_George19-May-08 2:22
George_George19-May-08 2:22 
GeneralRe: Dictionary Values copy Pin
J4amieC19-May-08 2:41
J4amieC19-May-08 2:41 
GeneralRe: Dictionary Values copy Pin
George_George19-May-08 2:49
George_George19-May-08 2:49 
GeneralRe: Dictionary Values copy Pin
J4amieC19-May-08 3:10
J4amieC19-May-08 3:10 
GeneralRe: Dictionary Values copy Pin
George_George19-May-08 3:18
George_George19-May-08 3:18 
AnswerRe: Dictionary Values copy Pin
leppie19-May-08 2:29
leppie19-May-08 2:29 
GeneralRe: Dictionary Values copy Pin
George_George19-May-08 2:34
George_George19-May-08 2:34 
GeneralRe: Dictionary Values copy Pin
J4amieC19-May-08 2:47
J4amieC19-May-08 2:47 
George_George wrote:
I think CopyTo will create new instances, other than just make a copy of the reference, right?


Buzz! Wrong Again!

Wink | ;)

Here's a little test app to convince you:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApplication2
{
    class Foo
    {
        public int abc;
        public Foo(int abc)
        {
            this.abc = abc;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {           

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

            Foo[] fooArray = new Foo[3];

            d.Values.CopyTo(fooArray, 0);

            Console.WriteLine(d[1].abc);
            Console.WriteLine(fooArray[0].abc);

            Console.WriteLine("Same Instance? {0}", (d[1] == fooArray[0]));
            Console.ReadLine();
        }
    }
}
</int,></int,>


Output:

10
10
Same Instance? True

This implies that I have picked the Foo instances with the same abc value (one from the dictionary one from the array), and when I check the for equality (which checks the reference not the value of abc it comes out true.
GeneralRe: Dictionary Values copy Pin
George_George19-May-08 2:52
George_George19-May-08 2:52 
GeneralRe: Dictionary Values copy Pin
leppie19-May-08 3:32
leppie19-May-08 3:32 
GeneralRe: Dictionary Values copy Pin
George_George19-May-08 4:26
George_George19-May-08 4:26 
QuestionRegarding XML loading [modified] Pin
manikandanid19-May-08 1:42
manikandanid19-May-08 1:42 
AnswerRe: Regarding XML loading [modified] Pin
J4amieC19-May-08 2:05
J4amieC19-May-08 2:05 
AnswerRe: Regarding XML loading Pin
Mike Dimmick19-May-08 2:25
Mike Dimmick19-May-08 2:25 
QuestionVS 2005 Image control Pin
Dextter19-May-08 1:33
Dextter19-May-08 1:33 
AnswerRe: VS 2005 Image control Pin
leppie19-May-08 2:30
leppie19-May-08 2:30 
QuestionSolution Structure Pin
mark_w_19-May-08 0:44
mark_w_19-May-08 0:44 
AnswerRe: Solution Structure Pin
Christian Graus19-May-08 0:50
protectorChristian Graus19-May-08 0:50 
GeneralRe: Solution Structure [modified] Pin
mark_w_19-May-08 0:52
mark_w_19-May-08 0:52 
AnswerRe: Solution Structure Pin
Simon P Stevens19-May-08 1:02
Simon P Stevens19-May-08 1:02 
AnswerRe: Solution Structure Pin
tgrt19-May-08 1:43
tgrt19-May-08 1:43 
QuestionGlobalization question Pin
Glen Harvy19-May-08 0:28
Glen Harvy19-May-08 0:28 
AnswerRe: Globalization question Pin
carbon_golem19-May-08 1:57
carbon_golem19-May-08 1:57 
GeneralRe: Globalization question Pin
Glen Harvy19-May-08 2:18
Glen Harvy19-May-08 2:18 
QuestionHow to create custom control Pin
Krish Agn19-May-08 0:23
Krish Agn19-May-08 0:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.