KEMBAR78
[API Proposal]: Uri : ISpanFormattable · Issue #87151 · dotnet/runtime · GitHub
Skip to content

[API Proposal]: Uri : ISpanFormattable #87151

@stephentoub

Description

@stephentoub

Background and motivation

Uri.ToString() is a common source of allocation, especially in logging code. We can avoid some such allocation by enabling Uri to play nicely with string interpolation by implementing ISpanFormattable that enables it to write directly into a destination buffer.

API Proposal

namespace System;

public class Uri : ISerializable,
+    ISpanFormattable
{
+    bool TryFormat(Span<char> destination, out int charsWritten);
}

API Usage

StringBuilder sb = ...;
sb.Append($"Url: {url}");

Alternative Designs

  • We could also implement IUtf8SpanFormattable to support directly writing out UTF8 bytes.

Risks

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions