I’m writing an app where I want to compare two strings’ size. By size, I mean the disk space they would take up if they were directly saved to the disk.
Is it possible to calculate the size of a string in C# without saving it to disk and checking the file information (unless it is more efficient to save it to disk)?
Yup, it’s easy:
Note that this is the number of bytes in the encoded form. If your file system is performing compression etc, it becomes much harder (or impossible) to predict the physical size taken.
You do have to pick an encoding though. There’s really no such thing as “directly” saving to disk – you’ve got to pick a binary representation.