public static int GetMaximumCharacterCount(this UTF8Encoding encoding) { var total = 0; foreach (KeyValuePair <int, int> codePoint in encoding.GetCodePoints()) { total += Math.Abs(codePoint.Value - codePoint.Key) + 1; } return(total - 2048); // 1112064 (1114112 minus 2048 code points for technically-invalid surrogate code points) https://en.wikipedia.org/wiki/UTF-8#cite_note-1 }