dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.62k stars 4.56k forks source link

[API Proposal]: Console.Write* convenience overloads for span #103500

Open stephentoub opened 1 month ago

stephentoub commented 1 month ago

Background and motivation

Console.Write and Console.WriteLine have a bunch of overloads for writing out various data types, but none for ReadOnlySpan<char>. We should add some purely for convenience. You can achieve the same by .ToString()'ing them or by using Console.Out.Write* instead, but it's just a little bit of unnecessary friction.

(I thought we already had an issue for this, but can't find it now.)

API Proposal

namespace System;

public static class Console
{
+   public static void Write(ReadOnlySpan<char> value);
+   public static void WriteLine(ReadOnlySpan<char> value);
}

API Usage

ReadOnlySpan<char> value = ...;
Console.WriteLine(value);

Alternative Designs

n/a

Risks

n/a

dotnet-policy-service[bot] commented 1 month ago

Tagging subscribers to this area: @dotnet/area-system-console See info in area-owners.md if you want to be subscribed.

neon-sunset commented 1 month ago

Does it make sense to also create overloads which accept DefaultInterpolatedStringHandler given that ReadOnlySpan<char> Text property on it is internal?

stephentoub commented 1 month ago

Why? There's already a string overload, so interpolation already works. You're concerned about performance with Console.WriteLine?

neon-sunset commented 1 month ago

Haha no, not of WriteLine itself, but given there's an option to make it allocation-free, I thought it could be useful to raise it.

stephentoub commented 2 weeks ago

Please feel free to open a separate issue.