ByteSize

Namespace: SnowBank.Buffers.Binary · struct

Implements: IEquatable<ByteSize>, IEquatable<long>, IEquatable<int>, IEquatable<ulong>, IEquatable<uint>, IFormattable

Represents the size (in bytes) of a buffer or range of data, with its associated "natural" unit (KB, KiB, GiB, ...)

Example

A size of 8 GiB can be expressed as (8589934592, GiB) so that the value can be nicely formated as "8 GiB".

Methods

Bytes

static ByteSize Bytes(int bytes)

Size expressed in bytes

static ByteSize Bytes(long bytes)

Size expressed in bytes

static ByteSize Bytes(uint bytes)

Size expressed in bytes

static ByteSize Bytes(ulong bytes)

Size expressed in bytes

Create

static ByteSize Create(int bytes, SizeUnit unit)

Size with a specific unit

static ByteSize Create(long bytes, SizeUnit unit)

Size with a specific unit

static ByteSize Create(uint bytes, SizeUnit unit)

Size with a specific unit

static ByteSize Create(ulong bytes, SizeUnit unit)

Size with a specific unit

Equals

bool Equals(object obj)

bool Equals(ByteSize other)

bool Equals(int other)

bool Equals(uint other)

bool Equals(long other)

bool Equals(ulong other)

ExaBytes

static ByteSize ExaBytes(int eb)

Size expressed in Petabytes (10^15 bytes)

static ByteSize ExaBytes(uint eb)

Size expressed in Petabytes (10^15 bytes)

ExiBytes

static ByteSize ExiBytes(int eib)

Size expressed in Exibytes (2^60 or 1,152,921,504,606,846,976 bytes)

static ByteSize ExiBytes(uint eib)

Size expressed in Exibytes (2^60 or 1,152,921,504,606,846,976 bytes)

FromUnitLiteral

static SizeUnit FromUnitLiteral(string literal)

Gets the size unit that corresponds to a string literal (ex: MB for "MB", GB for "GiB"

static SizeUnit FromUnitLiteral(ReadOnlySpan<char> literal)

Gets the size unit that corresponds to a string literal (ex: MB for "MB", GB for "GiB"

GetHashCode

int GetHashCode()

GetNaturalUnit

static SizeUnit GetNaturalUnit(ulong value)

GetUnitLiteral

static string GetUnitLiteral(SizeUnit unit)

Gets the string literal for a specific SizeUnit (ex: "MB" for MB, "GiB" for GB

GetUnitRatio

static double GetUnitRatio(SizeUnit unit)

GetUnitSize

static ulong GetUnitSize(SizeUnit unit)

GibiBytes

static ByteSize GibiBytes(int gib)

Size expressed in Gibibytes (2^30 or 1,073,741,824 bytes)

static ByteSize GibiBytes(uint gib)

Size expressed in Gibibytes (2^30 or 1,073,741,824 bytes)

static ByteSize GibiBytes(long gib)

Size expressed in Gibibytes (2^30 or 1,073,741,824 bytes)

static ByteSize GibiBytes(ulong gib)

Size expressed in Gibibytes (2^30 or 1,073,741,824 bytes)

GigaBytes

static ByteSize GigaBytes(int gb)

Size expressed in Gigabytes (10^9 bytes)

static ByteSize GigaBytes(uint gb)

Size expressed in Gigabytes (10^9 bytes)

static ByteSize GigaBytes(long gb)

Size expressed in Gigabytes (10^9 bytes)

static ByteSize GigaBytes(ulong gb)

Size expressed in Gigabytes (10^9 bytes)

IsMultipleOfUnit

bool IsMultipleOfUnit()

Test if the size is an exact multiple of its base unit

Returns: true if value mod sizeof(unit) == 0

new ByteSize(16384, SizeUnit.KiB).IsMultipleOfUnit() == true (because 16384 mod 1024 = 0)

new ByteSize(16384, SizeUnit.KB).IsMultipleOfUnit() == false (because 16384 mod 1000 = 384 != 0)

bool IsMultipleOfUnit(SizeUnit unit)

Test if the size is an exact multiple of the specified unit

Returns: true if value mod sizeof(unit) == 0

new ByteSize(16777216, SizeUnit.MiB).IsMultipleOfUnit(SizeOfUnit.MiB) == true (because 16777216 mod 1048576 = 0)

new ByteSize(16777216, SizeUnit.MiB).IsMultipleOfUnit(SizeOfUnit.KiB) == true (because 16777216 mod 1024 = 0)

new ByteSize(16777216, SizeUnit.MiB).IsMultipleOfUnit(SizeOfUnit.MB) == false (because 16777216 mod 1000000 = 777216 != 0)

new ByteSize(16000000, SizeUnit.KB).IsMultipleOfUnit(SizeOfUnit.KB) == true (because 16000000 mod 1000 = 0)

KibiBytes

static ByteSize KibiBytes(int kib)

Size expressed in Kibibytes (2^10 or 1,024 bytes)

static ByteSize KibiBytes(uint kib)

Size expressed in Kibibytes (2^10 or 1,024 bytes)

static ByteSize KibiBytes(long kib)

Size expressed in Kibibytes (2^10 or 1,024 bytes)

static ByteSize KibiBytes(ulong kib)

Size expressed in Kibibytes (2^10 or 1,024 bytes)

KiloBytes

static ByteSize KiloBytes(int kb)

Size expressed in Kilobytes (10^3 bytes)

static ByteSize KiloBytes(uint kb)

Size expressed in Kilobytes (10^3 bytes)

static ByteSize KiloBytes(long kb)

Size expressed in Kilobytes (10^3 bytes)

static ByteSize KiloBytes(ulong kb)

Size expressed in Kilobytes (10^3 bytes)

MebiBytes

static ByteSize MebiBytes(int mib)

Size expressed in Mebibytes (2^20 or 1,048,576 bytes)

static ByteSize MebiBytes(uint mib)

Size expressed in Mebibytes (2^20 or 1,048,576 bytes)

static ByteSize MebiBytes(long mib)

Size expressed in Mebibytes (2^20 or 1,048,576 bytes)

static ByteSize MebiBytes(ulong mib)

Size expressed in Mebibytes (2^20 or 1,048,576 bytes)

MegaBytes

static ByteSize MegaBytes(int mb)

Size expressed in Megabytes (10^6 bytes)

static ByteSize MegaBytes(uint mb)

Size expressed in Megabytes (10^6 bytes)

static ByteSize MegaBytes(long mb)

Size expressed in Megabytes (10^6 bytes)

static ByteSize MegaBytes(ulong mb)

Size expressed in Megabytes (10^6 bytes)

PebiBytes

static ByteSize PebiBytes(int pib)

Size expressed in Pebibytes (2^50 or 1,125,899,906,842,624 bytes)

static ByteSize PebiBytes(uint pib)

Size expressed in Pebibytes (2^50 or 1,125,899,906,842,624 bytes)

PetaBytes

static ByteSize PetaBytes(int pb)

Size expressed in Petabytes (10^15 bytes)

static ByteSize PetaBytes(uint pb)

Size expressed in Petabytes (10^15 bytes)

ScaleToUnitDouble

double ScaleToUnitDouble()

Returns the size in its unit

Returns: Corresponding value, expressed in the size unit. For example, if the size unit is KB then the result will be the size (in bytes) divided by 1024.

ByteSize.Create(1536, SizeUnit.KB).ScaleToUnitDouble() == 1.5d

double ScaleToUnitDouble(SizeUnit unit)

Returns the size, mapped to a specific unit

  • unit — Target unit

Returns: Corresponding value, expressed in the new unit. For example, if the unit is KB then the result will be the size (in bytes) divided by 1024.

ByteSize.Bytes(1536).ScaleToUnitDouble(SizeUnit.KB) == 1.5d

ScaleToUnitInt32

int ScaleToUnitInt32(MidpointRounding rounding = 1)

Returns the size in its unit, rounded to an integer

  • rounding — Rounding mode (defaults to AwayFromZero)

Returns: Corresponding value, expressed in the size unit. For example, if the size unit is KB then the result will be the size (in bytes) divided by 1024.

ByteSize.Bytes(1536).ScaleToUnitInt32(SizeUnit.KB) == 2

int ScaleToUnitInt32(SizeUnit unit, MidpointRounding rounding = 1)

Returns the size, mapped to a specific unit, rounded to an integer

  • unit — Target unit
  • rounding — Rounding mode (defaults to AwayFromZero)

Returns: Corresponding value, expressed in the new unit. For example, if the unit is KB then the result will be the size (in bytes) divided by 1024.

ByteSize.Bytes(1536).ScaleToUnitInt32(SizeUnit.KB) == 2

ScaleToUnitInt64

long ScaleToUnitInt64(MidpointRounding rounding = 1)

Returns the size in its unit, rounded to an integer

  • rounding — Rounding mode (defaults to AwayFromZero)

Returns: Corresponding value, expressed in the size unit. For example, if the size unit is KB then the result will be the size (in bytes) divided by 1024.

ByteSize.Bytes(1536).ScaleToUnitInt64(SizeUnit.KB) == 2

long ScaleToUnitInt64(SizeUnit unit, MidpointRounding rounding = 1)

Returns the size, mapped to a specific unit, rounded to an integer

  • unit — Target unit
  • rounding — Rounding mode (defaults to AwayFromZero)

Returns: Corresponding value, expressed in the new unit. For example, if the unit is KB then the result will be the size (in bytes) divided by 1024.

ByteSize.Bytes(1536).ScaleToUnitInt64(SizeUnit.KB) == 2

ScaleToUnitUInt32

uint ScaleToUnitUInt32(MidpointRounding rounding = 1)

Returns the size in its unit, rounded to an integer

  • rounding — Rounding mode (defaults to AwayFromZero)

Returns: Corresponding value, expressed in the size unit. For example, if the size unit is KB then the result will be the size (in bytes) divided by 1024.

ByteSize.Bytes(1536).ScaleToUnitUInt32(SizeUnit.KB) == 2

uint ScaleToUnitUInt32(SizeUnit unit, MidpointRounding rounding = 1)

Returns the size, mapped to a specific unit, rounded to an integer

  • unit — Target unit
  • rounding — Rounding mode (defaults to AwayFromZero)

Returns: Corresponding value, expressed in the new unit. For example, if the unit is KB then the result will be the size (in bytes) divided by 1024.

ByteSize.Bytes(1536).ScaleToUnitUInt32(SizeUnit.KB) == 2

ScaleToUnitUInt64

ulong ScaleToUnitUInt64(MidpointRounding rounding = 1)

Returns the size in its unit, rounded to an integer

  • rounding — Rounding mode (defaults to AwayFromZero)

Returns: Corresponding value, expressed in the size unit. For example, if the size unit is KB then the result will be the size (in bytes) divided by 1024.

ByteSize.Bytes(1536).ScaleToUnitUInt64(SizeUnit.KB) == 2

ulong ScaleToUnitUInt64(SizeUnit unit, MidpointRounding rounding = 1)

Returns the size, mapped to a specific unit, rounded to an integer

  • unit — Target unit
  • rounding — Rounding mode (defaults to AwayFromZero)

Returns: Corresponding value, expressed in the new unit. For example, if the unit is KB then the result will be the size (in bytes) divided by 1024.

ByteSize.Bytes(1536).ScaleToUnitUInt64(SizeUnit.KB) == 2

TebiBytes

static ByteSize TebiBytes(int tib)

Size expressed in Tebibytes (2^40 or 1,099,511,627,776 bytes)

static ByteSize TebiBytes(uint tib)

Size expressed in Tebibytes (2^40 or 1,099,511,627,776 bytes)

TeraBytes

static ByteSize TeraBytes(int tb)

Size expressed in Terabytes (10^12 bytes)

static ByteSize TeraBytes(uint tb)

Size expressed in Terabytes (10^12 bytes)

ToApproximateString

string ToApproximateString(bool base2 = false, IFormatProvider provider = null)

Returns a string representation of this instance, using its natural unit (with rounding if necessary).

ToExaBytes

double ToExaBytes()

Return the size, downscaled to Exabytes (EB, 10^19 bytes)

ToExiBytes

double ToExiBytes()

Return the size, downscaled to Exibytes (EiB, 2^60 bytes)

ToGibiBytes

double ToGibiBytes()

Return the size, downscaled to Gibibytes (GiB, 2^30 bytes)

ToGigaBytes

double ToGigaBytes()

Return the size, downscaled to Gigabytes (GB, 10^9 bytes)

ToInt32

int ToInt32()

Returns the value as a signed 32-bit integer

ToInt64

long ToInt64()

Returns the value as a signed 64-bit integer

ToKibiBytes

double ToKibiBytes()

Return the size, downscaled to Kibibytes (KiB, 2^10 bytes)

ToKiloBytes

double ToKiloBytes()

Return the size, downscaled to Kilobytes (KB, 10^3 bytes)

ToMebiBytes

double ToMebiBytes()

Return the size, downscaled to Mebibytes (MiB, 2^20 bytes)

ToMegaBytes

double ToMegaBytes()

Return the size, downscaled to Megabytes (MB, 10^6 bytes)

ToNaturalString

string ToNaturalString(IFormatProvider provider = null)

Returns a string representation of this instance, using the best unit (without rounding errors).

ToPebiBytes

double ToPebiBytes()

Return the size, downscaled to Pebibytes (PiB, 2^50 bytes)

ToPetaBytes

double ToPetaBytes()

Return the size, downscaled to Petabytes (PB, 10^15 bytes)

ToString

string ToString()

string ToString(string format, IFormatProvider provider)

ToTebiBytes

double ToTebiBytes()

Return the size, downscaled to Tebibytes (TiB, 2^40 bytes)

ToTeraBytes

double ToTeraBytes()

Return the size, downscaled to Terabytes (TB, 10^12 bytes)

ToUInt32

uint ToUInt32()

Returns the value as an unsigned 32-bit integer

ToUInt64

ulong ToUInt64()

Returns the value as an unsigned 64-bit integer

TryParse

static bool TryParse(ReadOnlySpan<char> literal, out ByteSize value)

Tries parsing a string representation of a size ("1 KB", "2 MiB", ...)

Fields

EB

const long EB

Exabyte (10^19 = 1,000,000,000,000,000,000 bytes)

EiB

const long EiB

Exibyte (2^60 = 1,152,921,504,606,846,976 bytes)

GB

const long GB

Gigabyte (10^9 = 1,000,000,000 bytes)

GiB

const long GiB

Gibibyte (2^30 = 1,073,741,824 bytes)

KB

const long KB

Kilobyte (10^3 = 1,000 bytes)

KiB

const long KiB

Kibibyte (2^10 = 1,024 bytes)

MB

const long MB

Megabyte (10^6 = 1,000,000 bytes)

MiB

const long MiB

Mebibyte (2^20 = 1,048,576 bytes)

PB

const long PB

Petabyte (10^15 = 1,000,000,000,000,000 bytes)

PiB

const long PiB

Pebibyte (2^50 = 1,125,899,906,842,624 bytes)

TB

const long TB

Terabyte (10^12 = 1,000,000,000,000 bytes)

TiB

const long TiB

Tebibyte (2^40 = 1,099,511,627,776 bytes)

Unit

readonly SizeUnit Unit

The natural unit that was used to construct this size (GiB, KB, ...)

Value

readonly ulong Value

The size (in bytes)

Zero

static readonly ByteSize Zero

Zero size (0 bytes)