I have a method which takes in a reference double and returns a string, while also modifying the value of the reference:
const long OneKb = 1024;
const long OneMb = OneKb * 1024;
const long OneGb = OneMb * 1024;
const long OneTb = OneGb * 1024;
public string GetLargestDataSizeAndUnitOfMeasurement(ref double value, int decimalPlaces = 0)
{
var asTb = Math.Round(value / OneTb, decimalPlaces);
var asGb = Math.Round(value / OneGb, decimalPlaces);
var asMb = Math.Round(value / OneMb, decimalPlaces);
var asKb = Math.Round(value / OneKb, decimalPlaces);
string unit = asTb > 1 ? string.Format("Tb", value = asTb)
: asGb > 1 ? string.Format("Gb", value = asGb)
: asMb > 1 ? string.Format("Mb", value = asMb)
: asKb > 1 ? string.Format("Kb", value = asKb)
: string.Format("B", value = Math.Round(value, decimalPlaces));
return unit;
}
My question is whether it's acceptable to assign the reference a new value within the string.Format(), despite the value not being pertinent to that method itself. I could execute the if separately to modify value if I wanted to avoid this, but this seems cleaner and potentially more efficient at scale.