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

Partial bytes do not exist #67

Open Rainmaker52 opened 2 years ago

Rainmaker52 commented 2 years ago

I'm trying to use this library to keep my code a bit more readable.

If I do this:

var memoryBuffer = MemoryPool<byte>.Shared.Rent((int) ByteSize.FromKibiBytes(128).Bytes);

I have to cast .Bytes to an integer, because the Bytes property is a double. I can understand returning a double for almost anything. But seeing as "Bytes" in particular, is really the lowest addressable data type, there is no way to have "partial bytes". You would never be addressing "1.2 bytes".

I would therefore suggest changing the "Bytes" type to an uint or ulong (I really don't see negative bytes as a thing either, but I could be wrong there).

RayCarrot commented 2 years ago

The library supports bits which don't have to be a multiple of 8 (and thus giving you a fractional bytes count). I don't really see the purpose of that though if I'm being honest and would agree that having the bytes be an integer would make more sense.

sandrock commented 2 years ago

(int) ByteSize.FromKibiBytes(128).Bytes

Int32 goes up to 2147483647. That's 2 GiB. That would not be a good candidate to represent file sizes.

You should not use the int type to represent file size.

Rainmaker52 commented 1 year ago

(int) ByteSize.FromKibiBytes(128).Bytes

Int32 goes up to 2147483647. That's 2 GiB. That would not be a good candidate to represent file sizes.

You should not use the int type to represent file size.

Yes. As I mentioned, I suggest unit or ulong

The cast, in this case, was necessary because of the function signature requiring a signed int. Which is stupid, but not under my control.