Class PrefixTableExtensions
Extension methods for the PrefixTable class.
Examples
Lookup specific SI and binary prefix scales from a table to obtain scaled representations at each available magnitude. The setup prerequisite is a unit value large enough to have multiple valid prefix scales (for example, 1E21 watts or 1 TiB of bytes). This example creates MetricPrefixTable and DataPrefixTable instances (setup phase), then demonstrates the lookup workflow by calling available *OrDefault() extension methods to check which prefix scales exist at each magnitude level. The outcome displays all available prefix representations organized by scale category (SI decimal large/small scales, binary data scales, and miscellaneous decimal scales), allowing you to see the complete prefix transformation options. In production code, you would typically call only the specific *OrDefault() methods relevant to your domain (for example, KiloOrDefault() and MebiOrDefault()) to avoid unnecessary allocations and focus on the prefix scales needed for your use case.
using System;
using Codebelt.Unitify;
namespace Unitify.Samples;
public class PrefixTableExtensionsExample
{
public static void Main()
{
// Create a metric prefix table from a large watt value
var wattUnit = UnitFactory.CreateWatt(1E21); // Very large value in watts
var table = new MetricPrefixTable(wattUnit);
// QuettaOrDefault: Scale the unit to the quetta (10^30) prefix, returning null if the value
// does not have a valid quetta representation. Use this for extremely large scientific values.
Console.WriteLine("=== SI Decimal Prefixes (Large Scales) ===");
var quetta = table.QuettaOrDefault();
if (quetta != null) Console.WriteLine($"Quetta: {quetta}");
// ZettaOrDefault: Scale the unit to the zetta (10^21) prefix, returning null if unavailable.
// Common for very large data or energy quantities in scientific computing.
var zetta = table.ZettaOrDefault();
if (zetta != null) Console.WriteLine($"Zetta: {zetta}");
// YottaOrDefault: Scale the unit to the yotta (10^24) prefix, returning null if not applicable.
// Used for astronomical or theoretical quantities exceeding exabytes or exawatts.
var yotta = table.YottaOrDefault();
if (yotta != null) Console.WriteLine($"Yotta: {yotta}");
// Common SI decimal prefixes: Scale from very large (mega, giga) to medium scales.
// KiloOrDefault, MegaOrDefault, GigaOrDefault, TeraOrDefault, and PetaOrDefault cover
// the most frequently used commercial and engineering scales.
var kilo = table.KiloOrDefault();
if (kilo != null) Console.WriteLine($"Kilo: {kilo}");
var mega = table.MegaOrDefault();
if (mega != null) Console.WriteLine($"Mega: {mega}");
var giga = table.GigaOrDefault();
if (giga != null) Console.WriteLine($"Giga: {giga}");
var tera = table.TeraOrDefault();
if (tera != null) Console.WriteLine($"Tera: {tera}");
var peta = table.PetaOrDefault();
if (peta != null) Console.WriteLine($"Peta: {peta}");
var exa = table.ExaOrDefault();
if (exa != null) Console.WriteLine($"Exa: {exa}");
// Small SI decimal prefixes: Scale from milli (10^-3) down to quecto (10^-30).
// These are essential for medical, laboratory, and precision manufacturing applications.
// MilliOrDefault and MicroOrDefault are the most common for everyday engineering.
Console.WriteLine("\n=== SI Decimal Prefixes (Small Scales) ===");
var milli = table.MilliOrDefault();
if (milli != null) Console.WriteLine($"Milli: {milli}");
var micro = table.MicroOrDefault();
if (micro != null) Console.WriteLine($"Micro: {micro}");
var nano = table.NanoOrDefault();
if (nano != null) Console.WriteLine($"Nano: {nano}");
var pico = table.PicoOrDefault();
if (pico != null) Console.WriteLine($"Pico: {pico}");
var femto = table.FemtoOrDefault();
if (femto != null) Console.WriteLine($"Femto: {femto}");
var atto = table.AttoOrDefault();
if (atto != null) Console.WriteLine($"Atto: {atto}");
var zepto = table.ZeptoOrDefault();
if (zepto != null) Console.WriteLine($"Zepto: {zepto}");
var yocto = table.YoctoOrDefault();
if (yocto != null) Console.WriteLine($"Yocto: {yocto}");
var ronto = table.RontoOrDefault();
if (ronto != null) Console.WriteLine($"Ronto: {ronto}");
var quecto = table.QuectoOrDefault();
if (quecto != null) Console.WriteLine($"Quecto: {quecto}");
// Binary prefix scales in a data table: Scale from kibi (2^10) through yobi (2^80).
// QuebiOrDefault and RobiOrDefault represent the newest binary scales (added in 2022).
// Use binary prefixes exclusively for data storage and network bandwidth to avoid
// confusion with decimal SI scales; 1 KiB = 1024 bytes, while 1 kB = 1000 bytes.
Console.WriteLine("\n=== Binary Prefixes (Data Table) ===");
var byteUnit = UnitFactory.CreateByte(1099511627776); // 1 TiB
var dataTable = new DataPrefixTable(byteUnit);
// QuebiOrDefault: Scale to the quebi (2^100) prefix for data storage.
// Used in theoretical or future-scale data center planning.
var quobi = dataTable.QuebiOrDefault();
if (quobi != null) Console.WriteLine($"Quebi: {quobi}");
// RobiOrDefault: Scale to the robi (2^90) prefix, the second-newest binary scale.
var robi = dataTable.RobiOrDefault();
if (robi != null) Console.WriteLine($"Robi: {robi}");
// Common binary prefixes: KibiOrDefault (1024 bytes), MebiOrDefault (1 million bytes),
// GibiOrDefault (1 billion bytes), and TebiOrDefault (1 trillion bytes) are the most
// commonly encountered in modern storage and memory specifications.
var kibi = dataTable.KibiOrDefault();
if (kibi != null) Console.WriteLine($"Kibi: {kibi}");
var mebi = dataTable.MebiOrDefault();
if (mebi != null) Console.WriteLine($"Mebi: {mebi}");
var gibi = dataTable.GibiOrDefault();
if (gibi != null) Console.WriteLine($"Gibi: {gibi}");
var tebi = dataTable.TebiOrDefault();
if (tebi != null) Console.WriteLine($"Tebi: {tebi}");
var pebi = dataTable.PebiOrDefault();
if (pebi != null) Console.WriteLine($"Pebi: {pebi}");
var exbi = dataTable.ExbiOrDefault();
if (exbi != null) Console.WriteLine($"Exbi: {exbi}");
var zebi = dataTable.ZebiOrDefault();
if (zebi != null) Console.WriteLine($"Zebi: {zebi}");
var yobi = dataTable.YobiOrDefault();
if (yobi != null) Console.WriteLine($"Yobi: {yobi}");
// Additional decimal prefixes: CentiOrDefault, DeciOrDefault, DecaOrDefault, and HectoOrDefault
// cover non-standard but occasionally used scales in specialized fields like chemistry
// and older engineering references. RonnaOrDefault and RontoOrDefault are the newest decimal scales.
Console.WriteLine("\n=== Additional Decimal Prefixes ===");
var centi = table.CentiOrDefault();
if (centi != null) Console.WriteLine($"Centi: {centi}");
var deci = table.DeciOrDefault();
if (deci != null) Console.WriteLine($"Deci: {deci}");
var deca = table.DecaOrDefault();
if (deca != null) Console.WriteLine($"Deca: {deca}");
var hecto = table.HectoOrDefault();
if (hecto != null) Console.WriteLine($"Hecto: {hecto}");
var ronna = table.RonnaOrDefault();
if (ronna != null) Console.WriteLine($"Ronna: {ronna}");
}
}
public static class PrefixTableExtensions
- Inheritance
-
PrefixTableExtensions
Methods
AttoOrDefault(PrefixTable)
Returns the unit with the Atto prefix or the default value if not found.
public static IPrefixUnit AttoOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Atto prefix or the default value.
CentiOrDefault(PrefixTable)
Returns the unit with the Centi prefix or the default value if not found.
public static IPrefixUnit CentiOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Centi prefix or the default value.
DecaOrDefault(PrefixTable)
Returns the unit with the Deca prefix or the default value if not found.
public static IPrefixUnit DecaOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Deca prefix or the default value.
DeciOrDefault(PrefixTable)
Returns the unit with the Deci prefix or the default value if not found.
public static IPrefixUnit DeciOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Deci prefix or the default value.
ExaOrDefault(PrefixTable)
Returns the unit with the Exa prefix or the default value if not found.
public static IPrefixUnit ExaOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Exa prefix or the default value.
ExbiOrDefault(PrefixTable)
Returns the unit with the Exbi prefix or the default value if not found.
public static IPrefixUnit ExbiOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Exbi prefix or the default value.
FemtoOrDefault(PrefixTable)
Returns the unit with the Femto prefix or the default value if not found.
public static IPrefixUnit FemtoOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Femto prefix or the default value.
GibiOrDefault(PrefixTable)
Returns the unit with the Gibi prefix or the default value if not found.
public static IPrefixUnit GibiOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Gibi prefix or the default value.
GigaOrDefault(PrefixTable)
Returns the unit with the Giga prefix or the default value if not found.
public static IPrefixUnit GigaOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Giga prefix or the default value.
HectoOrDefault(PrefixTable)
Returns the unit with the Hecto prefix or the default value if not found.
public static IPrefixUnit HectoOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Hecto prefix or the default value.
KibiOrDefault(PrefixTable)
Returns the unit with the Kibi prefix or the default value if not found.
public static IPrefixUnit KibiOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Kibi prefix or the default value.
KiloOrDefault(PrefixTable)
Returns the unit with the Kilo prefix or the default value if not found.
public static IPrefixUnit KiloOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Kilo prefix or the default value.
MebiOrDefault(PrefixTable)
Returns the unit with the Mebi prefix or the default value if not found.
public static IPrefixUnit MebiOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Mebi prefix or the default value.
MegaOrDefault(PrefixTable)
Returns the unit with the Mega prefix or the default value if not found.
public static IPrefixUnit MegaOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Mega prefix or the default value.
MicroOrDefault(PrefixTable)
Returns the unit with the Micro prefix or the default value if not found.
public static IPrefixUnit MicroOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Micro prefix or the default value.
MilliOrDefault(PrefixTable)
Returns the unit with the Milli prefix or the default value if not found.
public static IPrefixUnit MilliOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Milli prefix or the default value.
NanoOrDefault(PrefixTable)
Returns the unit with the Nano prefix or the default value if not found.
public static IPrefixUnit NanoOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Nano prefix or the default value.
PebiOrDefault(PrefixTable)
Returns the unit with the Pebi prefix or the default value if not found.
public static IPrefixUnit PebiOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Pebi prefix or the default value.
PetaOrDefault(PrefixTable)
Returns the unit with the Peta prefix or the default value if not found.
public static IPrefixUnit PetaOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Peta prefix or the default value.
PicoOrDefault(PrefixTable)
Returns the unit with the Pico prefix or the default value if not found.
public static IPrefixUnit PicoOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Pico prefix or the default value.
QuebiOrDefault(PrefixTable)
Returns the unit with the Quebi prefix or the default value if not found.
public static IPrefixUnit QuebiOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Quebi prefix or the default value.
QuectoOrDefault(PrefixTable)
Returns the unit with the Quecto prefix or the default value if not found.
public static IPrefixUnit QuectoOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Quecto prefix or the default value.
QuettaOrDefault(PrefixTable)
Returns the unit with the Quetta prefix or the default value if not found.
public static IPrefixUnit QuettaOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Quetta prefix or the default value.
RobiOrDefault(PrefixTable)
Returns the unit with the Robi prefix or the default value if not found.
public static IPrefixUnit RobiOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Robi prefix or the default value.
RonnaOrDefault(PrefixTable)
Returns the unit with the Ronna prefix or the default value if not found.
public static IPrefixUnit RonnaOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Ronna prefix or the default value.
RontoOrDefault(PrefixTable)
Returns the unit with the Ronto prefix or the default value if not found.
public static IPrefixUnit RontoOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Ronto prefix or the default value.
TebiOrDefault(PrefixTable)
Returns the unit with the Tebi prefix or the default value if not found.
public static IPrefixUnit TebiOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Tebi prefix or the default value.
TeraOrDefault(PrefixTable)
Returns the unit with the Tera prefix or the default value if not found.
public static IPrefixUnit TeraOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Tera prefix or the default value.
YobiOrDefault(PrefixTable)
Returns the unit with the Yobi prefix or the default value if not found.
public static IPrefixUnit YobiOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Yobi prefix or the default value.
YoctoOrDefault(PrefixTable)
Returns the unit with the Yocto prefix or the default value if not found.
public static IPrefixUnit YoctoOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Yocto prefix or the default value.
YottaOrDefault(PrefixTable)
Returns the unit with the Yotta prefix or the default value if not found.
public static IPrefixUnit YottaOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Yotta prefix or the default value.
ZebiOrDefault(PrefixTable)
Returns the unit with the Zebi prefix or the default value if not found.
public static IPrefixUnit ZebiOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Zebi prefix or the default value.
ZeptoOrDefault(PrefixTable)
Returns the unit with the Zepto prefix or the default value if not found.
public static IPrefixUnit ZeptoOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Zepto prefix or the default value.
ZettaOrDefault(PrefixTable)
Returns the unit with the Zetta prefix or the default value if not found.
public static IPrefixUnit ZettaOrDefault(this PrefixTable prefixes)
Parameters
prefixesPrefixTableThe table of multiple units.
Returns
- IPrefixUnit
The unit with the Zetta prefix or the default value.