dotnet / runtime

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

[API Proposal]: Convenient integers reading #74489

Closed vorotynsky closed 1 year ago

vorotynsky commented 2 years ago

Background and motivation

C# doesn't have an API to read rows of integers from the console nicely.

One of the solutions is:

var array = Console.ReadLine()!.Split(' ', StringSplitOptions.RemoveEmptyEntries);
array = Array.ConvertAll(array, Int32.Parse);

Such a code is necessary for solving problems at codeforces or olympiads.

API Proposal

namespace System.IO;

public static class ReaderExtensions
{
    public static Int32 ReadInt32(this TextReader reader) { throw null; }

    // methods for other types
}

API Usage

var array = new int[10];

for (var i = 0; i < 10; i++)
    array[i] = Console.In.ReadInt32();

Alternative Designs

C++ operators

int arr[10];

for (int i = 0; i < 10; i++)
    std::cin >> arr[i];

Java's Scanner

Scanner scanner = new Scanner(System.in);
int[] arr = new int[10];

for (int i = 0; i < 10; i++)
    arr[i] = scanner.nextInt();

Text format

C has function scanf, and F#, Go has the same API.

C# can adopt it like this:

Console.In.Scanf("%i", ref arr[i]);

Just read a word

for (int i = 0; i < 10; i++)
    arr[i] = Int32.Parse(Console.ReadWord());

Risks

No response

dotnet-issue-labeler[bot] commented 2 years 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.

ghost commented 2 years ago

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

Issue Details
### Background and motivation C# doesn't have an API to read rows of integers from the console nicely. One of the solutions is: ```csharp var array = Console.ReadLine()!.Split(' ', StringSplitOptions.RemoveEmptyEntries); array = Array.ConvertAll(array, Int32.Parse); ``` Such a code is necessary for solving problems at codeforces or olympiads. ### API Proposal ```csharp namespace System.IO; public static class ReaderExtensions { public static Int32 ReadInt32(this TextReader reader) { throw null; } // methods for other types } ``` ### API Usage ```csharp var array = new int[10]; for (var i = 0; i < 10; i++) array[i] = Console.In.ReadInt32(); ``` ### Alternative Designs #### C++ operators ```cpp int arr[10]; for (int i = 0; i < 10; i++) std::cin >> arr[i]; ``` #### Java's `Scanner` ```java Scanner scanner = new Scanner(System.in); int[] arr = new int[10]; for (int i = 0; i < 10; i++) arr[i] = scanner.nextInt(); ``` #### Text format C has function `scanf`, and F#, Go has the same API. C# can adopt it like this: ```csharp Console.In.Scanf("%i", ref arr[i]); ``` #### Just read a word ```csharp for (int i = 0; i < 10; i++) arr[i] = Int32.Parse(Console.ReadWord()); ``` ### Risks _No response_
Author: vorotynsky
Assignees: -
Labels: `api-suggestion`, `area-System.IO`, `untriaged`
Milestone: -
svick commented 2 years ago

Related: https://github.com/dotnet/runtime/issues/64621, which proposes a different API with the same goal.

adamsitnik commented 1 year ago

Hi @vorotynsky

Thank you for your proposal. Since #64621 was opened first and includes proposal for other types as well I am going to close this issue as a duplicate. Please follow #64621 for updates.