1

Is there an equivalent type for Bytes, just like System.Web.UI.WebControls.Unit for units?

I imagine a struct which takes the byte count as long and two calculated properties:

  • enumeration type with the highest multiple >= 1 (B, KB, MB, etc..)
  • decimal with the value in the above multiple

Implementing such struct is far from difficult but i'd rather use something from the framework

Below i paste a sample to better explain my intent

my draft proposal

public struct ByteUnit
{
   public long ByteCount {get; set; }
   public ByteMultiple Multiple {get; }
   public decimal ByteCountInMultiple {get; }
}

Is there a framework type for such a thing?

Or doesn't it even make any sense and i am missing the big picture?

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76

1 Answers1

2

No, there is no such type in .Net Framework and unlikely to have one (in part due to controversial nature of M/K multipliers - there are at least 3 meaning for it: programmers, SI and marketing).

You may look into 2 other approaches:

  • separate presentation of data. I.e. you can have custom formatting options for you type (type contains just data like 12333 bytes) and than format String.Format("M as custom format - {0:M}", valueOfMyType)
  • check out other questions on "units of measurement" topic (see results of C# unit measurment search here like Units of measure in C# - almost). Also see how it is done in F# for more inspirations. This approach may be useful if you have many similar values with "unit of measurement" like mass, speed, volume, temperature.
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Thanks. From what i understood you're saying to implement a IFormattable class with the presentation logic, is that it? – Luis Filipe Jan 20 '14 at 10:44
  • 1
    @LuisFilipe - yes - to me it looks like your goal is just formatting. I can't imagine why data portion of "byte count" would behave differently unlike in `Unit` case where something like "percentage" and "pixels" would mean very different thing on data level. – Alexei Levenkov Jan 20 '14 at 18:56