Repository navigation
OnixLabs.Core.Text.EncodingExtensions
Provides extension methods for Encoding instances. These methods are exposed through a C# 14 extension block over a receiver of type Encoding?, so they may be invoked even when the receiver is null.
| Namespace | OnixLabs.Core.Text |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → EncodingExtensions
|
[EditorBrowsable(EditorBrowsableState.Never)]
public static class EncodingExtensionsNote This class is decorated with
[EditorBrowsable(EditorBrowsableState.Never)], which hides it from IntelliSense completion lists in some IDE configurations. The extension methods remain fully public and callable; they primarily support the Base value codecs and other internal text formatting throughout the library.
EncodingExtensions adds null-safe convenience methods over Encoding? receivers. Because the class uses a C# 14 extension(Encoding? receiver) block, all methods are accessible on any Encoding variable regardless of whether its value is null. The GetOrDefault() method acts as the null-coalescing pivot, and all other methods call through it so the caller never needs to guard against a null encoding.
Bring the methods into scope with:
using OnixLabs.Core.Text;| Method | Returns | Description |
|---|---|---|
GetOrDefault() |
Encoding |
Gets the current encoding, or Encoding.UTF8 if the receiver is null. |
GetBytes(ReadOnlySpan<char>) |
byte[] |
Obtains a byte array from the specified character span using the current or default encoding. |
public Encoding GetOrDefault()Gets the current Encoding, or the default encoding if the current Encoding is null. The default encoding is Encoding.UTF8.
Returns — The receiving Encoding, or Encoding.UTF8 when the receiver is null.
using OnixLabs.Core.Text;
using System.Text;
Encoding? maybeNull = null;
Encoding encoding = maybeNull.GetOrDefault(); // Encoding.UTF8
Encoding ascii = Encoding.ASCII.GetOrDefault(); // Encoding.ASCIIpublic byte[] GetBytes(ReadOnlySpan<char> value)Obtains a byte array from the specified ReadOnlySpan<char> instance, encoding the characters using the receiving Encoding, or Encoding.UTF8 when the receiver is null (see GetOrDefault()).
| Parameter | Type | Description |
|---|---|---|
value |
ReadOnlySpan<char> |
The value from which to obtain a byte array. |
Returns — A byte array containing the encoded representation of value.
using OnixLabs.Core.Text;
using System.Text;
byte[] utf8 = Encoding.UTF8.GetBytes("ABC"); // [ 0x41, 0x42, 0x43 ]
Encoding? fallback = null;
byte[] viaDefault = fallback.GetBytes("ABC"); // encoded as UTF-8ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions