I have a class that has 3 string properties. I want to store these in a list so that when I make changes to the strings of the list they also get updated in the class.
This would be easy to do if I was using class object, but string seems to behave differently. It seems to make a copy of the object for the list rather then have a pointer to the object. How am I supposed to do this is C#? If this is not possible is there a better way?
The problem with
strings is that they are immutable. In other words, you can never change astringonce it is created.Thus, if you want to ‘change’ a
string, you must remove the original from theList, and store the result back into the list. Example:That code does nothing because the
stringais pointing to never changes. It just points to a newstring.