UnicodeEncoding.GetMaxByteCount(Int32) 메서드

정의

지정된 문자 수를 인코딩하여 생성되는 최대 바이트 수를 계산합니다.

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

매개 변수

charCount
Int32

인코딩할 문자 수입니다.

반품

지정된 문자 수를 인코딩하여 생성되는 최대 바이트 수입니다.

예외

charCount가 0보다 작습니다.

-또는-

결과 바이트 수가 정수로 반환될 수 있는 최대 수보다 큽니다.

대체가 발생했습니다(자세한 내용은 .NET의 문자 인코딩 참조).

-그리고-

EncoderFallbackEncoderExceptionFallback로 설정됩니다.

예제

다음 예제에서는 메서드를 사용하여 GetMaxByteCount 지정된 수의 문자를 인코딩하는 데 필요한 최대 바이트 수를 반환하는 방법을 보여 줍니다.

using System;
using System.Text;

class UnicodeEncodingExample {
    public static void Main() {
        UnicodeEncoding Unicode = new UnicodeEncoding();
        int charCount = 2;
        int maxByteCount = Unicode.GetMaxByteCount(charCount);
        Console.WriteLine(
            "Maximum of {0} bytes needed to encode {1} characters.",
            maxByteCount,
            charCount
        );
    }
}
Imports System.Text

Class UnicodeEncodingExample
    
    Public Shared Sub Main()
        Dim uni As New UnicodeEncoding()
        Dim charCount As Integer = 2
        Dim maxByteCount As Integer = uni.GetMaxByteCount(charCount)
        Console.WriteLine("Maximum of {0} bytes needed to encode {1} characters.", maxByteCount, charCount)
    End Sub
End Class

설명

결과 바이트를 저장하는 데 필요한 GetBytes 정확한 배열 크기를 계산하기 위해 애플리케이션에서 사용합니다 GetByteCount. 최대 배열 크기를 계산하려면 애플리케이션에서 .를 사용해야 GetMaxByteCount합니다. 메서드는 GetByteCount 일반적으로 더 적은 메모리를 할당하지만 메서드는 GetMaxByteCount 일반적으로 더 빠르게 실행됩니다.

GetMaxByteCount 는 현재 선택된 최악의 경우를 포함하여 최악의 경우를 검색합니다 EncoderFallback. 잠재적으로 큰 문자열 GetMaxByteCount 을 사용하여 대체를 선택하는 경우 큰 값을 반환할 수 있습니다.

대부분의 경우 이 메서드는 작은 문자열에 대해 적절한 숫자를 검색합니다. 큰 문자열의 경우 매우 큰 버퍼를 사용하고 좀 더 합리적인 버퍼를 초과하는 드문 경우에서 오류를 catch하는 중에서 선택해야 할 수 있습니다. 사용 또는 GetByteCount.를 사용하는 Encoder.Convert 다른 방법을 고려할 수도 있습니다.

GetMaxByteCount 에 대한 관계가 GetChars없습니다. 애플리케이션에서 사용할 GetChars유사한 함수가 필요한 경우 해당 함수를 사용해야 GetMaxCharCount합니다.

메모

GetMaxByteCount(N) 가 반드시 .와 N* GetMaxByteCount(1)같은 값은 아닙니다.

적용 대상

추가 정보