I think this question was asked many times in C# but my problem is maybe more solvable.
I have an object value as a string : myobject.value and I want to store this value in a queue (or anything else) to access it later. Is it possible?
I read a lot of posts saying that it is not possible to store a ref to a string.
I don’t see any solution to store a ref to my string value myobject.value and change it later.
Any Ideas ?
If you want to store a reference to a string that people can see and share the changes of you will need to wrap it in a class:
Then people use this instead of a string:
Even though strings are reference types, they are immutable so changes need to the “copied” around. Sharing is this way doesn’t change the immutability, it just centrally holds one copy of a string that everyone is looking at via their reference to
Wrapped<string>.You can take the
Wrapped<T>class further and give it implicit cast support to and fromT, for a little syntactic sugar: