Date Published: November 22, 2020
MyType firstObject = new MyType();
string myXmlString = XMLHelper.Serialize(firstObject);
MyType secondObject = XMLHelper.Deserialize<MyType>(myXmlString);
So, for starters, what's happening here?
Very simple
Q. why don't we just copy firstObject into secondObject and job done - as so:
MyType secondObject = firstObject;
A. Reference types are not easy to copy.
What does that mean? and what is a reference type?
There are two types of variables in C# - reference types and value types - and the difference is whether the variable stores the actual value, or the variable stores a reference to the value.
That was quite a mouthful - let's explain that a little better.
Supposing we declare a variable and assign it a value - like this:
var myVariable = 45;
If myVariable is a value type, then myVariable stores 45. That's why it's called a value type - because it stores a value. Quite simple.
However if myVariable is a reference type, then myVariable stores a reference to the value. A reference means - something that tells you where you can get the value from, so it refers to the value. That's why it's called a reference type - becuase it stores a reference to the value. Also simple.
(As an aside, if you google around about value types and reference types, you'll find a lot written about the stack and the heap. It turns out though that all of that is mostly irrelevant to developers - see this great writeup from Eric Lippert).
Truthful answer is that when you are retreiving the value, it makes very little difference. Where it does make a big difference though, is where you want to make a copy of your variable - like so:
var secondVariable = firstVariable;
Copying a variable copies whatever is stored in the first variable to the second variable. Take that in a minute.
Now we get to the problem - if we have a reference type variable, and we need to copy the value (not just the reference) because we don't want the copy to be affected by changes to the original - (this is called a deep-copy) - how can we do this?
ANSWER (or to be more precise, one of the anwers) - this odd code from above:
MyType firstObject = new MyType();
string myXmlString = XMLHelper.Serialize(firstObject);
MyType secondObject = XMLHelper.Deserialize<MyType>(myXmlString);
We break the link between firstObject and secondObject by serializing firstObject into a different format (xml) and then deserializing it back into secondObject.
Now secondObject is a deep-copy of firstObject.
The values might be identical, but they are totally unconnected and each variable contains a different reference - each to their own value.
That makes sense now.
Thanks for reading!