I have a client application that connects to a WCF service, I get the file size from the server as a long value then I convert it at the client to string so it appears like ex:52.21 MB
The application gets too many files sizes every time user changes the current directory path.
My question: Should I convert the values to string format from the WCF service app then return it to the client as a string or should I just return the size as a long value and let the client to convert it to a string format
In other way which value take more bytes in the memory:
long size = 55050240;
string size = "52.5 MB";
long large_size = 56371445760;
string large_size = "52.5 GB";
UPDATE:
I use this method to convert long value to a string format:
private string ConvertUnit(long source)
{
const int byteConversion = 1024;
double bytes = Convert.ToDouble(source);
if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
{
return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB");
}
else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range
{
return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB");
}
else if (bytes >= byteConversion) //KB Range
{
return string.Concat(Math.Round(bytes / byteConversion, 2), " KB");
}
else //Bytes
{
return string.Concat(bytes, " Bytes");
}
}
Short question: Which takes more memory, string value or long value?
Having a service layer that output values that are formatted is a very bad idea. What happens if the requirements change and rather than a GB suffix, the client application UI needs to output in bytes? e.g.
56,371,445,760? This will required the client application to parse then re-format the data sent by the server.Also, determining the memory required to store an individual variable is easy. What is not so easy is to work out the overall impact to your application. Different types will be handled differently in your code, and as a result a smaller variable might incur a greater cost.