The number of bytes for the Byte Order Mark („BOM“) differs among the used encodings.
public static bool IsBomRelevantEncoding( Encoding encoding, out int bomSizeBytes ) { if ( encoding == Encoding.Unicode ) { bomSizeBytes = 2; return true; } else if ( encoding == Encoding.UTF32 ) { bomSizeBytes = 4; return true; } else if ( encoding == Encoding.UTF8 ) { bomSizeBytes = 3; return true; } else if ( encoding == Encoding.UTF7 ) { bomSizeBytes = 4; return true; } else { bomSizeBytes = 0; return false; } }
References:
- „Byte order mark“ in Wikipedia
- System.Text.Encoding in the MSDN documentation