diff --git a/csharp/src/Google.Protobuf/WritingPrimitives.cs b/csharp/src/Google.Protobuf/WritingPrimitives.cs index 030f06c7e1a9..8ad483042260 100644 --- a/csharp/src/Google.Protobuf/WritingPrimitives.cs +++ b/csharp/src/Google.Protobuf/WritingPrimitives.cs @@ -164,7 +164,6 @@ public static void WriteBool(ref Span buffer, ref WriterInternalState stat /// public static void WriteString(ref Span buffer, ref WriterInternalState state, string value) { -#if !NETSTANDARD1_1 const int MaxBytesPerChar = 3; const int MaxSmallStringLength = 128 / MaxBytesPerChar; @@ -174,10 +173,11 @@ public static void WriteString(ref Span buffer, ref WriterInternalState st // This saves calling GetByteCount on the string. We get the string length from GetBytes. if (value.Length <= MaxSmallStringLength && buffer.Length - state.position - 1 >= value.Length * MaxBytesPerChar) { + // Get the original position, then increment it on state by 1, then write string to buffer. + // Method will return byte length, which is then set to the original position. buffer[state.position++] = (byte)WriteStringToBuffer(buffer, ref state, value); return; } -#endif int length = Utf8Encoding.GetByteCount(value); WriteLength(ref buffer, ref state, length);