omar / ByteSize

ByteSize is a utility class that makes byte size representation in code easier by removing ambiguity of the value being represented. ByteSize is to bytes what System.TimeSpan is to time.
MIT License
558 stars 50 forks source link

Formattable ToString exception in 2.1.0 #60

Closed RayCarrot closed 2 years ago

RayCarrot commented 3 years ago

In the 2.1.0 release the ByteSize class had the IFormattable implemented and a new ToString overload to go along with it. This causes a NullReferenceException to occur in this scenario:

$"{new ByteSize()}"

Due to the string interpolation being used without any formatting specified the format value in the ToString method overload gets set to null. Since the ToString method doesn't have a null check this will fail. If instead a ToString() is explicitly used it will work since the default ToString method overload gets used which specifies the default format string:

$"{new ByteSize()}.ToString()"

I'm thinking the easiest solution would be to have the ToString method check if the format value is null and if so default it to "0.##". For now I'm going to have to roll back the version to 2.0.0 since this has broken a lot for me.

RayCarrot commented 3 years ago

Just noticed there's a pull request open now which seems to fix the problem: #58. Would be great if it could be merged!

omar commented 2 years ago

Fixed in #58.