If I run
string myString = "*.txt";
Print("sizeof(char): " + sizeof(char) + " bytes");
Print("myString.Length * sizeof(char): " + (myString.Length * sizeof(char)) + " bytes");
It will print
sizeof(char): 2 bytes
myString.Length * sizeof(char): 10 bytes
But, if I run the code from the first answer to this question:
myString = "*.txt"
long size = 0;
using (Stream s = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(s, myString);
size = s.Length;
}
Print("myString Serialized Size: " + size + " bytes");
I get
myString Serialized Size: 29 bytes
Which of these is a more accurate representation of how much space my string is taking up in memory?