0

I am making an ArrayList of Bar Object. I then want to be able to modify the BarType member contained in the ArrayList. I created a shortened version of my code that actually is doing what I want it to do, but I do not understand why it is doing it.

In the code I create two Bar objects and stuff them into a ArrayList. I pull the last Bar Object out of the list cast it to Bar. But TMP is a new instance of the BAR object, yet when I change the value in TMP it changes the values in my arraylist. I am not sure why it does it or if this is the best way to change the BarType value in an ArrayList.

Below is the code:

ArrayList barArray = new ArrayList();

Bar myBar = new Bar(10,20);

barArray.Add(myBar); // I want to create and keep adding Bar object to my arraylist

myBar = new Bar(30, 40);

barArray.Add(myBar);

Bar tmp = new Bar(5,6);
tmp = (Bar)barArray[myBar];  // extracting the last Bar Object
tmp.BarType = true;
tmp.High = 100;              // too my surprise this change the value in the ArrayList

So below is my Bar class:

public class Bar 
{
    public double High, Low;
    public bool BarType;      // I want to be able to modify this variable

    public Bar(double Low, double High)
    {
        this.Low = Low; this.High = High;
    }
}
1
  • 5
    Any reason you need to be tortured with using ArrayList instead of List<Bar>? Commented Mar 2, 2015 at 22:33

1 Answer 1

2

But TMP is a new instance of the BAR object, yet when I change the value in TMP it changes the values in my arraylist

This is incorrect. This would be the case if you were using struct Bar or value-types/copy-constructors in C++, but in C#/.NET all class objects are heap-allocated and referred to by references, which in turn are passed by-value (which is to say, the references are passed by-value).

To get the copy-on-assignment behaviour you're expecting, change class Bar to struct Bar (or implement IClonable and use .Clone() to get the new Bar instance).

Here you're creating a new instance of Bar on the heap, accessed by the reference named tmp

Bar tmp = new Bar(5,6);

But here, you're overwriting the reference in tmp with a new reference to the last item in barArray. The original tmp Bar instance has no references pointing to it and will be garbage-collected.

tmp = (Bar)barArray[c];

tmp is now a reference (essentially an alias) to an item in BarArray:

tmp.BarType = true;
tmp.High = 100;

...which is equivalent to:

barArray[c].BarType = true;
barArray[c].High = 100;

Also, protip: only types (class, struct, etc) should have TitleCase names along with public members (methods, properties, etc). Local variables and parameters should be in camelCase. Fields should also either be camelCase or _underscorePrefixed at your discretion.

Sign up to request clarification or add additional context in comments.

4 Comments

Objects aren't passed at all - references are passed, by value by default. Saying that "class objects are passed by reference" is misleading. (In particular, it suggests that using ref for a parameter whose type is a class is pointless - but it's not.) See pobox.com/~skeet/csharp/parameters.html
@JonSkeet "References are passed by-value" is a confusing concept to beginners though as then we're debating English semantics. By "passing by-reference" I mean "assigned by-reference" or "assignment is only of references" but I struggle to find a way to describe this that isn't re-using words that have deeper meanings, such as "reference".
Use the words that are accurate then - which is "arguments are passed by value by default, and the value of a reference-type variable is a reference". I've explained this concept to hundreds of beginners over time - and while it takes a little bit more time (ideally with real-world analogies), it's much better than leaving people with a confused notion of what pass-by-reference means, in my experience... I've had to clean up the mess of people being confused by that. It's not a matter of debating English semantics - it's a matter of using well-understood computer science terms properly.
@JonSkeet I've re-worded my explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.