Repository: dotnet/runtime
File: src/libraries/System.Private.CoreLib/src/System/IO/TextWriter.cs (C#)

Code before:
                {
                    var t = (TupleSlim<TextWriter, char, char>)state!;
                    t.Item1.Write(t.Item2);
                    t.Item1.WriteLine(t.Item3);
                }, new TupleSlim<TextWriter, char, char>(this, valueChars[0], valueChars[1]), CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
            }

            return WriteLineAsync(valueChars[0]);
        }

        public virtual Task WriteLineAsync(string? value) =>
            Task.Factory.StartNew(static state =>
            {
                var t = (TupleSlim<TextWriter, string?>)state!;
                t.Item1.WriteLine(t.Item2);
            }, new TupleSlim<TextWriter, string?>(this, value), CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

        /// <summary>
        /// Writes a string followed by a line terminator to the text stream asynchronously.
        /// </summary>
        /// <param name="value">The string to write. If <paramref name="value"/> is <see langword="null"/>, only the line terminator is written.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns>A task that represents the asynchronous write operation.</returns>
        public Task WriteLineAsync(string? value, CancellationToken cancellationToken) =>
            cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
            value == null ? WriteLineAsync(cancellationToken) :
            WriteLineAsync(value.AsMemory(), cancellationToken);

        /// <summary>
        /// Equivalent to WriteLineAsync(stringBuilder.ToString()) however it uses the
        /// StringBuilder.GetChunks() method to avoid creating the intermediate string
        /// </summary>
        /// <param name="value">The string (as a StringBuilder) to write to the stream</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        public virtual Task WriteLineAsync(StringBuilder? value, CancellationToken cancellationToken = default)
        {
            return
                cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
                value == null ? WriteAsync(CoreNewLine, cancellationToken) :
                WriteLineAsyncCore(value, cancellationToken);

            async Task WriteLineAsyncCore(StringBuilder sb, CancellationToken ct)
            {
                foreach (ReadOnlyMemory<char> chunk in sb.GetChunks())
                {
                    await WriteAsync(chunk, ct).ConfigureAwait(false);
                }
                await WriteAsync(CoreNewLine, ct).ConfigureAwait(false);
            }
        }


Review comment (suggestion):
@copilot, these two lines should be removed, such that it just always delegates to the memory overload.

Code after:
                    t.Item1.Write(t.Item2);
                    t.Item1.WriteLine(t.Item3);
                }, new TupleSlim<TextWriter, char, char>(this, valueChars[0], valueChars[1]), CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
            }

            return WriteLineAsync(valueChars[0]);
        }

        public virtual Task WriteLineAsync(string? value) =>
            Task.Factory.StartNew(static state =>
            {
                var t = (TupleSlim<TextWriter, string?>)state!;
                t.Item1.WriteLine(t.Item2);
            }, new TupleSlim<TextWriter, string?>(this, value), CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

        /// <summary>
        /// Writes a string followed by a line terminator to the text stream asynchronously.
        /// </summary>
        /// <param name="value">The string to write. If <paramref name="value"/> is <see langword="null"/>, only the line terminator is written.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns>A task that represents the asynchronous write operation.</returns>
        public Task WriteLineAsync(string? value, CancellationToken cancellationToken) =>
            WriteLineAsync(value.AsMemory(), cancellationToken);

        /// <summary>
        /// Equivalent to WriteLineAsync(stringBuilder.ToString()) however it uses the
        /// StringBuilder.GetChunks() method to avoid creating the intermediate string
        /// </summary>
        /// <param name="value">The string (as a StringBuilder) to write to the stream</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        public virtual Task WriteLineAsync(StringBuilder? value, CancellationToken cancellationToken = default)
        {
            return
                cancellationToken.IsCancellationRequested ? Task.FromCanceled(cancellationToken) :
                value == null ? WriteAsync(CoreNewLine, cancellationToken) :
                WriteLineAsyncCore(value, cancellationToken);

            async Task WriteLineAsyncCore(StringBuilder sb, CancellationToken ct)
            {
                foreach (ReadOnlyMemory<char> chunk in sb.GetChunks())
                {
                    await WriteAsync(chunk, ct).ConfigureAwait(false);
                }
                await WriteAsync(CoreNewLine, ct).ConfigureAwait(false);
            }
        }

        public Task WriteLineAsync(char[]? buffer)
        {
            if (buffer == null)
            {