Lire en anglais Modifier

Partager via


UTF8Encoding.Preamble Property

Definition

Gets a Unicode byte order mark encoded in UTF-8 format, if this object is configured to supply one.

public override ReadOnlySpan<byte> Preamble { get; }

Property Value

A byte span containing the Unicode byte order mark, if this object is configured to supply one; otherwise, the default span.

Remarks

The UTF8Encoding object can provide a preamble, which is a span of bytes that can be prefixed to the sequence of bytes that result from the encoding process. Prefacing a sequence of encoded bytes with a byte order mark (code point U+FEFF) helps the decoder determine the byte order and the transformation format, or UTF. The Unicode byte order mark (BOM) is serialized as 0xEF 0xBB 0xBF. Note that the Unicode Standard neither requires nor recommends the use of a BOM for UTF-8 encoded streams.

You can instantiate a UTF8Encoding object whose Preamble is a valid BOM in the following ways:

  • By retrieving the UTF8Encoding object returned by the Encoding.UTF8 property.

  • By calling a UTF8Encoding constructor with an encoderShouldEmitUTF8Identifier parameter and setting its value set to true.

All other UTF8Encoding objects are configured to return a default span rather than a valid BOM.

The BOM provides nearly certain identification of an encoding for files that otherwise have lost a reference to their encoding, such as untagged or improperly tagged web data or random text files stored when a business did not have international concerns. Often user problems might be avoided if data is consistently and properly tagged.

For standards that provide an encoding type, a BOM is somewhat redundant. However, it can be used to help a server send the correct encoding header. Alternatively, it can be used as a fallback in case the encoding is otherwise lost.

There are some disadvantages to using a BOM. For example, knowing how to limit the database fields that use a BOM can be difficult. Concatenation of files can be a problem also, for example, when files are merged in such a way that an unnecessary character can end up in the middle of data. In spite of the few disadvantages, however, the use of a BOM is highly recommended.

For more information on byte order and the byte order mark, see The Unicode Standard at the Unicode home page.

Important

To ensure that the encoded bytes are decoded properly when they are saved as a file or as a stream, you can prefix the beginning of a stream of encoded bytes with a preamble. Note that the GetBytes method does not prepend a BOM to a sequence of encoded bytes; supplying a BOM at the beginning of an appropriate byte stream is the developer's responsibility.

Applies to

Produit Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9