List<string>

  • Thread starter Thread starter Lubomir
  • Start date Start date
L

Lubomir

Hi,

I want to store string members in a generic list. When I assign to the
member of a list a new value, I would like it would be reflected in the
collection also:

string firstString= "first";
myList.Add(firstString);

firstString="second";
string test = myList[0];

I wanted the test would have the value "second".

Of course this is not true because strings are immutable. Is there any
clever way how to achieve this effect?

Thanks,
Lubomir
 
Lubomir said:
I want to store string members in a generic list. When I assign to the
member of a list a new value, I would like it would be reflected in the
collection also:

string firstString= "first";
myList.Add(firstString);

firstString="second";
string test = myList[0];

I wanted the test would have the value "second".

Of course this is not true because strings are immutable. Is there any
clever way how to achieve this effect?

No, it's not because strings are immutable. It's because you've changed
the value of the variable to be a reference to a different object.
You'll see the same thing with StringBuilder, which certainly isn't
immutable:

StringBuilder builder = new StringBuilder("first");
myList.Add(builder);

builder = new StringBuilder("second");
Console.WriteLine(myList[0]);

That will still print out "first".

Now, if you change the contents of the object referred to by an element
in the list (e.g. calling builder.Append("second") instead of giving it
a new value) then that's a different matter.
 
Thanks.


Jon Skeet said:
Lubomir said:
I want to store string members in a generic list. When I assign to the
member of a list a new value, I would like it would be reflected in the
collection also:

string firstString= "first";
myList.Add(firstString);

firstString="second";
string test = myList[0];

I wanted the test would have the value "second".

Of course this is not true because strings are immutable. Is there any
clever way how to achieve this effect?

No, it's not because strings are immutable. It's because you've changed
the value of the variable to be a reference to a different object.
You'll see the same thing with StringBuilder, which certainly isn't
immutable:

StringBuilder builder = new StringBuilder("first");
myList.Add(builder);

builder = new StringBuilder("second");
Console.WriteLine(myList[0]);

That will still print out "first".

Now, if you change the contents of the object referred to by an element
in the list (e.g. calling builder.Append("second") instead of giving it
a new value) then that's a different matter.
 
Hi,

I want to store string members in a generic list. When I assign to the
member of a list a new value, I would like it would be reflected in the
collection also:

string firstString= "first";
myList.Add(firstString);

firstString="second";
string test = myList[0];

I wanted the test would have the value "second".

Of course this is not true because strings are immutable. Is there any
clever way how to achieve this effect?

Thanks,
Lubomir

You could wrap the string in a class and store that in the list, like
so:

myList = new List<Foo>;
Foo foo = new Foo();
foo.bar = "first";
myList.Add(foo);
foo.bar = "second";
Console.WriteLine(myList[0].bar);

You would be using the reference to the class instead of a direct
reference to the string, so while the assignment of the string would
still return a new reference, the class would keep track of that.

hth,
Kevin Wienhold
 
Back
Top