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

デコードするバイト数。

戻り値

指定したバイト数をデコードすることによって生成される最大文字数。

例外

byteCount が 0 未満です。

- または -

結果の文字数が、int として返すことのできる最大数を超えています。

フォールバックが発生しました (詳細については「.NET での文字エンコード」を参照)

および

DecoderFallbackDecoderExceptionFallback に設定されます。

次のコード例では、 メソッドを 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する必要があります。

Note

GetMaxCharCount(N)はと同じ値であるとは限りません N* GetMaxCharCount(1)

適用対象

こちらもご覧ください