Let's say I have an object like so:
public class MyObject
{
string MyString {get;set;}
public MyObject DeepCopy()
{
return new MyObject { MyString = ??? } // string.copy(MyString), or MyString ?
}
}
The goal is to cache this object (IMemoryCache
). Is it safe to use MyString = MyString
? Or will it's reference point to unallocated memory when the scope of MyObject will end?
I am wondering, as the string itself is immutable, so if the reference stays valid, it seems like a good idea to not copy each character (string.copy
), which might be costly for nothing.