UTF7Encoding.GetMaxCharCount(Int32) 方法

定義

計算將指定數目的位元組解碼所產生的最大字元數。

public:
 override int GetMaxCharCount(int byteCount);
public override int GetMaxCharCount (int byteCount);
override this.GetMaxCharCount : int -> int
Public Overrides Function GetMaxCharCount (byteCount As Integer) As Integer

參數

byteCount
Int32

要解碼的位元組數。

傳回

Int32

解碼指定位元組數所產生的最大字元數。

例外狀況

byteCount 小於零。

-或- 所產生的字元數目大於可用整數傳回的數目上限。

發生後援 (如需詳細資訊,請參閱 .NET 中的字元編碼) -和- DecoderFallback 設定為 DecoderExceptionFallback

範例

下列程式碼範例示範如何使用 GetMaxCharCount 方法,傳回譯指定位元組數目所產生的最大字元數。

using namespace System;
using namespace System::Text;
int main()
{
   UTF7Encoding^ utf7 = gcnew UTF7Encoding;
   int byteCount = 8;
   int maxCharCount = utf7->GetMaxCharCount( byteCount );
   Console::WriteLine( "Maximum of {0} characters needed to decode {1} bytes.", maxCharCount, byteCount );
}
using System;
using System.Text;

class UTF7EncodingExample {
    public static void Main() {
        UTF7Encoding utf7 = new UTF7Encoding();
        int byteCount = 8;
        int maxCharCount = utf7.GetMaxCharCount(byteCount);
        Console.WriteLine(
            "Maximum of {0} characters needed to decode {1} bytes.",
            maxCharCount,
            byteCount
        );
    }
}
Imports System.Text

Class UTF7EncodingExample
    
    Public Shared Sub Main()
        Dim utf7 As New UTF7Encoding()
        Dim byteCount As Integer = 8
        Dim maxCharCount As Integer = utf7.GetMaxCharCount(byteCount)
        Console.WriteLine( _
            "Maximum of {0} characters needed to decode {1} bytes.", _
            maxCharCount, _
            byteCount _
        )
    End Sub
End Class

備註

若要計算儲存所產生字元所需的 GetChars 確切陣列大小,應用程式會使用 GetCharCount 。 若要計算陣列大小上限,應用程式應該使用 GetMaxCharCount 。 方法 GetCharCount 通常允許配置較少的記憶體,而 GetMaxCharCount 方法通常會執行得更快。

GetMaxCharCount 是最差大小寫的數位,包括目前選取 DecoderFallback 的最差大小寫。 如果選擇具有可能大型字串的後援, GetMaxCharCount 可以傳回大值。

在大部分情況下,此方法會傳回小型字串的合理數位。 對於大型字串,您可能必須在使用非常大的緩衝區和攔截錯誤之間做選擇,在罕見的情況下,會超過更合理的緩衝區。 您也可以考慮使用 GetCharCountDecoder.Convert 的不同方法。

GetMaxCharCount 與 沒有關聯 GetBytes 。 如果您的應用程式需要與 搭配 GetBytes 使用的類似函式,它應該使用 GetMaxByteCount

注意

GetMaxCharCount(N) 不一定與 相同的值 N* GetMaxCharCount(1)

適用於

另請參閱