dotnet / runtime

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

[API Proposal]: Support array format string #80014

Open Cricle opened 1 year ago

Cricle commented 1 year ago

Background and motivation

In current

using System;

namespace My;

internal class Program
{
    static void Main()
    {
        var arr = new int[] { 1, 2, 3 };
        Console.WriteLine(arr);//System.Int32[]
    }
}

Will it support call ToString print {1,2,3}?

In source https://source.dot.net/#System.Private.CoreLib/src/System/Array.CoreCLR.cs,156e066ecc4ccedf https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Array.cs,156e066ecc4ccedf

No override ToString method.

API Proposal

namespace System;

public class Array
{
    public override ToString();
    //Or no object method
   public void FormatString();
}

API Usage

var arr = new int[] { 1, 2, 3 };
Console.WriteLine(arr);

Will print { 1, 2, 3 }

In object array, will call object.ToString() method to be a string of elements.

Alternative Designs

It will break changed for call Array.ToString()

But generally no one uses it like this, right?

Risks

When want get array type, it only can call Array.GetType().FullName

dotnet-issue-labeler[bot] commented 1 year ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

svick commented 1 year ago

Previous discussion: https://github.com/dotnet/runtime/issues/425.

ghost commented 1 year ago

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

Issue Details
### Background and motivation In current ```csharp using System; namespace My; internal class Program { static void Main() { var arr = new int[] { 1, 2, 3 }; Console.WriteLine(arr);//System.Int32[] } } ``` Will it support call `ToString` print `{1,2,3}`? In source https://source.dot.net/#System.Private.CoreLib/src/System/Array.CoreCLR.cs,156e066ecc4ccedf https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Array.cs,156e066ecc4ccedf No override `ToString` method. ### API Proposal ```csharp namespace System; public class Array { public override ToString(); //Or no object method public void FormatString(); } ``` ### API Usage ```csharp var arr = new int[] { 1, 2, 3 }; Console.WriteLine(arr); ``` Will print `{ 1, 2, 3 }` In object array, will call `object.ToString()` method to be a string of elements. ### Alternative Designs It will break changed for call `Array.ToString()` But generally no one uses it like this, right? ### Risks When want get array type, it only can call `Array.GetType().FullName`
Author: Cricle
Assignees: -
Labels: `api-suggestion`, `area-System.Runtime`, `untriaged`
Milestone: -