C
C#3y ago
hanu

A way to get an integer input quickly?

Recently i'm trying to do competitive programming with C# and i'm curious if there's a way to get an (array of) integer input. The method I all know is this
// for an array of integers
int[] arrayInput = Console.ReadLine().Split(' ').Select(Int32.Parse).ToArray()
// for one integer
int singleInput = Int32.Parse(Console.ReadLine())
// for an array of integers
int[] arrayInput = Console.ReadLine().Split(' ').Select(Int32.Parse).ToArray()
// for one integer
int singleInput = Int32.Parse(Console.ReadLine())
6 Replies
Angius
Angius3y ago
That's the way
hanu
hanuOP3y ago
is this the fastest? or the same mechanism as other stdin functions like scanf in C/C++?
Angius
Angius3y ago
Assuming all input is correct, I can't think of a more performant way
hanu
hanuOP3y ago
hm got it
Angius
Angius3y ago
I guess benchmarking .Select() vs a loop would be one thing Or you could play around with Console.Read() and try assembling the numbers yourself, based on the chars the user enters Might want to ask in #advanced as well, there might be some super low-level way of reading the chars directly from the buffer or something
hanu
hanuOP3y ago
okay i got curious if there's some low-level thingy that i might be able to try thanks

Did you find this page helpful?