wierd behavior

M

Muthu Arumugam

Tried the following c# code

static void Main(string[] args)

{

ArrayList list = new ArrayList();

int i = 10;

string s = "Test";

list.Add(i);

list.Add(s);

// run it without/with commenting the following 2 lines

s = "hello";

i = 90;

Console.WriteLine(string.Format("{0}, {1}\n", i, s));

Console.WriteLine(list.Contains(i));

Console.ReadKey();

}



It has real wierd behavior when you change values in array list with the
object variables and see if you can use Contains or IndexOf(). They give
wrong results even though i look for the objects with their variables....

any thoughts ? is it a bug ?

muthu

http://techblog.muthuka.com/
 
M

Muthu Arumugam

updated code:
ArrayList list = new ArrayList();

int i = 10;

string s = "Test";

list.Add(i);

list.Add(s);

// run it without/with commenting the following 2 lines

s = "hello";

i = 90;

Console.WriteLine(string.Format("{0}, {1}\n", i, s));

Console.WriteLine(list.Contains(i) + "\n");

IEnumerator en = list.GetEnumerator();

while (en.MoveNext())

{

Console.WriteLine(en.Current);

}

Console.ReadKey();
 
J

Jon Skeet [C# MVP]

It has real wierd behavior when you change values in array list with the
object variables and see if you can use Contains or IndexOf(). They give
wrong results even though i look for the objects with their variables....

any thoughts ? is it a bug ?

No, it's not a bug. The ArrayList doesn't contain the variables, it
contains the values the variables had when you called Add.
 
P

Peter Duniho

Muthu Arumugam said:
[...]
It has real wierd behavior when you change values in array list with the
object variables and see if you can use Contains or IndexOf(). They give
wrong results even though i look for the objects with their variables....

any thoughts ? is it a bug ?

It has perfectly normal behavior. Not weird at all.

When you add i to the ArrayList, you are not adding a reference to the
variable i. You are adding a reference to a newly created object containing
the value 10.

When you add s to the ArrayList, you are not adding a reference to the
variable s. You are adding the same reference to "Test" that s contains at
that moment.

When you change i, you don't change the value of the object that the
ArrayList references. You just change i. Likewise, when you change s, you
don't change the reference that the ArrayList references. You just change
the reference that s references.

If you want the content of the ArrayList to change when you change something
else, you need to add to ArrayList a reference to some mutable object that
you have access to.

I haven't looked too closely at how boxing works, but it's possible that if
you kept the Object reference created when you add i to the ArrayList:

Object obj;

obj = i;
list.Add(obj);

you could then change the value contained by obj, which would also be
reflected in the reference to obj that the ArrayList has. I don't recall
whether boxed data is mutable or not.

As far as the string goes, you could keep the reference to "Test", but since
string objects are immutable, there's no way to modify the data that
reference refers to. You would need some different kind of object that
essentially contains a string, but which is mutable (maybe StringBuilder
would work for you in that case).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top