Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters.
GetByteCount(ReadOnlySpan<Char>, Boolean) |
When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters in the 'chars' span. A parameter indicates whether to clear the internal state of the encoder after the calculation. |
GetByteCount(Char*, Int32, Boolean) |
When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer. A parameter indicates whether to clear the internal state of the encoder after the calculation. |
GetByteCount(Char[], Int32, Int32, Boolean) |
When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters from the specified character array. A parameter indicates whether to clear the internal state of the encoder after the calculation. |
This method does not affect the state of the encoder.
To calculate the exact array size that GetBytes requires to store the resulting bytes, the application should use GetByteCount.
If GetBytes
is called with flush
set to false
, the encoder stores trailing characters at the end of the data block in an internal buffer and uses them in the next encoding operation. The application should call GetByteCount
on a block of data immediately before calling GetBytes
on the same block, so that any trailing characters from the previous block are included in the calculation.
When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters in the 'chars' span. A parameter indicates whether to clear the internal state of the encoder after the calculation.
public:
virtual int GetByteCount(ReadOnlySpan<char> chars, bool flush);
public virtual int GetByteCount(ReadOnlySpan<char> chars, bool flush);
abstract member GetByteCount : ReadOnlySpan<char> * bool -> int
override this.GetByteCount : ReadOnlySpan<char> * bool -> int
Public Overridable Function GetByteCount (chars As ReadOnlySpan(Of Char), flush As Boolean) As Integer
A character span to encode.
true
to simulate clearing the internal state of the encoder after the calculation; otherwise, false
.
The number of bytes produced by encoding the specified characters and any characters in the internal buffer.
Product | Versions |
---|---|
.NET | Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Standard | 2.1 |
Important
This API is not CLS-compliant.
When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters starting at the specified character pointer. A parameter indicates whether to clear the internal state of the encoder after the calculation.
public:
virtual int GetByteCount(char* chars, int count, bool flush);
[System.CLSCompliant(false)]
public virtual int GetByteCount(char* chars, int count, bool flush);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual int GetByteCount(char* chars, int count, bool flush);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual int GetByteCount(char* chars, int count, bool flush);
[<System.CLSCompliant(false)>]
abstract member GetByteCount : nativeptr<char> * int * bool -> int
override this.GetByteCount : nativeptr<char> * int * bool -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member GetByteCount : nativeptr<char> * int * bool -> int
override this.GetByteCount : nativeptr<char> * int * bool -> int
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Security.SecurityCritical>]
abstract member GetByteCount : nativeptr<char> * int * bool -> int
override this.GetByteCount : nativeptr<char> * int * bool -> int
A pointer to the first character to encode.
The number of characters to encode.
true
to simulate clearing the internal state of the encoder after the calculation; otherwise, false
.
The number of bytes produced by encoding the specified characters and any characters in the internal buffer.
chars
is null
(Nothing
in Visual Basic .NET).
count
is less than zero.
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
Fallback is set to EncoderExceptionFallback.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
When overridden in a derived class, calculates the number of bytes produced by encoding a set of characters from the specified character array. A parameter indicates whether to clear the internal state of the encoder after the calculation.
public:
abstract int GetByteCount(cli::array <char> ^ chars, int index, int count, bool flush);
public abstract int GetByteCount(char[] chars, int index, int count, bool flush);
abstract member GetByteCount : char[] * int * int * bool -> int
Public MustOverride Function GetByteCount (chars As Char(), index As Integer, count As Integer, flush As Boolean) As Integer
The character array containing the set of characters to encode.
The index of the first character to encode.
The number of characters to encode.
true
to simulate clearing the internal state of the encoder after the calculation; otherwise, false
.
The number of bytes produced by encoding the specified characters and any characters in the internal buffer.
chars
is null
.
index
or count
is less than zero.
-or-
index
and count
do not denote a valid range in chars
.
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
Fallback is set to EncoderExceptionFallback.
The following code example demonstrates how to use the GetByteCount method to return the number of bytes required to encode an array of characters using a Unicode Encoder.
using System;
using System.Text;
class EncoderExample {
public static void Main() {
// Unicode characters.
Char[] chars = new Char[] {
'\u0023', // #
'\u0025', // %
'\u03a0', // Pi
'\u03a3' // Sigma
};
Encoder uniEncoder = Encoding.Unicode.GetEncoder();
int byteCount = uniEncoder.GetByteCount(chars, 0, chars.Length, true);
Console.WriteLine(
"{0} bytes needed to encode characters.", byteCount
);
}
}
/* This example produces the following output.
8 bytes needed to encode characters.
*/
Imports System.Text
Imports Microsoft.VisualBasic.Strings
Class EncoderExample
Public Shared Sub Main()
' Unicode characters.
' ChrW(35) = #
' ChrW(37) = %
' ChrW(928) = Pi
' ChrW(931) = Sigma
Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
Dim uniEncoder As Encoder = Encoding.Unicode.GetEncoder()
Dim byteCount As Integer = _
uniEncoder.GetByteCount(chars, 0, chars.Length, True)
Console.WriteLine("{0} bytes needed to encode characters.", byteCount)
End Sub
End Class
'
'This example produces the following output.
'
'8 bytes needed to encode characters.
'
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in