views:

140

answers:

5

Sorry, I am being both thick and lazy, but mostly lazy. Actually, not even that. I am trying to save time so I can do more in less time as there's a lot to be done.

Does this copy the reference or the actual object data?

public class Foo
{
    private NameValueCollection _nvc = null;

    public Foo( NameValueCollection nvc)
    {
        _nvc = nvc;
    }
}

public class Bar
{
    public static void Main()
    {
        NameValueCollection toPass = new NameValueCollection();
        new Foo( toPass ); // I believe this only copies the reference
                           // so if I ever wanted to compare toPass and 
                           // Foo._nvc (assuming I got hold of the private 
                           // field using reflection), I would only have to
                           // compare the references and wouldn't have to compare
                           // each string (deep copy compare), right?
}

I think I know the answer for sure: it only copies the reference. But I am not even sure why I am asking this.

I guess my only concern is, if, after instantiating Foo by calling its parameterized ctor with toPass, if I needed to make sure that the NVC I passed as toPass and the NVC private field _nvc had the exact same content, I would just need to compare their references, right?

+3  A: 

Yes, that's correct. However, if you want to compare toPass and Foo._nvc later, you still may want to do a member-wise comparison so different but equivalent collections compare equal.

Matthew Flaschen
Thanks, Matthew. The `toPass` and the operation of newing `Foo` with it will be a one-off and my equivalence comparison must also be a one-off, so I won't have to deal with the situation you describe, but you do make a very interesting point.
Water Cooler v2
+1  A: 

In a word, yes.

Steven Robbins
Many thanks, Steven.
Water Cooler v2
A: 

Edit: Yes, this copied the value of the reference.

I also agree with Matthew Flaschen on deep copy compare - and also overload your equality compare operator

VoodooChild
reason for downvote?
VoodooChild
It's *not* `ByRef`. The C# equivalent to that is the `ref` keyword. If it *was* `ref`, the constructor could affect the value of `toPass` in `Main`.
Matthew Flaschen
when he does "new Foo( toPass )", is he not sending the obj's reference down to be copied to "_nvc"? And is this not why when he compares these two variables will result in equal? what did I miss?
VoodooChild
He is passing the *value* of the reference. Again, VB.NET ByRef is C# ref.
Matthew Flaschen
k Thanks, I will remember this from now..
VoodooChild
A: 

You are passing a reference type by value. You can change the data pointed to by the reference (e.g. add new items to the collection) and the changes will be reflected to the caller. As Matthew pointed out, you may still need to check for different yet-equivalent objects.

Assuming you stick to the usage you printed and do not perform any "new" operations the object references will remain equivalent.

public class Foo 
    { 
        public NameValueCollection _nvc = null;

        public Foo( NameValueCollection nvc) 
        {
            //this is cool
            _nvc = nvc;
            _nvc.Add("updated", "content");

            //this isn't cool. Now you have a new object with equivalent members and you should follow Matthew's advice
            //_nvc = new NameValueCollection();
            //_nvc.Add("foo", "bar");
            //_nvc.Add("updated", "content");
        } 
    }

    public class Bar
    {
        public static void Main()
        {
            NameValueCollection toPass = new NameValueCollection();
            toPass.Add("foo", "bar");
            Foo f = new Foo(toPass); 

            if (Object.Equals(toPass, f._nvc))
                Console.WriteLine("true");
            else
                Console.WriteLine("false");
            Console.ReadLine();
        }
    }
P.Brian.Mackey
Thank you for the short tutorial. It fortifies my understanding of the topic.
Water Cooler v2
A: 

An excellent article by Jon Skeet, the Guru on value type and reference type

ram