When you start solving DSA or competitive programming problems in C#, one of the first confusing things is input handling.
If you come from C++, you may be used to writing:
cin >> H >> X >> Y;In C#, there is no direct equivalent that works exactly the same way.
Instead, you will often see something like:
string[] input = Console.ReadLine().Split();
long H = long.Parse(input[0]);
long X = long.Parse(input[1]);
long Y = long.Parse(input[2]);At first, this looks unnecessarily complicated.
Why do we need an array?
Why do we need .Split()?
And what exactly does long.Parse() do?
Let’s understand it step by step.
Step 1: What does Console.ReadLine() return?
Consider this input:
10 20 30Now write:
string input = Console.ReadLine();The value stored inside input is:
"10 20 30"Notice something important.
Even though we entered numbers, C# reads the complete line as a string.
So:
Console.ReadLine()does not return three numbers.
It returns one string containing everything written on that line.
Think of it like this:
Keyboard Input
↓
10 20 30
↓
Console.ReadLine()
↓
"10 20 30"Step 2: Why do we use .Split()?
We want three separate values:
10
20
30But right now we have:
"10 20 30"So we need to separate the values.
That is the job of .Split().
string[] input = Console.ReadLine().Split();Now C# divides the string based on whitespace.
Conceptually:
"10 20 30"
↓
.Split()
↓
["10", "20", "30"]The values are stored in an array:
input[0] // "10"
input[1] // "20"
input[2] // "30"This explains why we use an array.
The array is simply holding the separate pieces produced by .Split().
Step 3: Why can't we use these values directly?
At this point:
input[0]contains:
"10"But "10" is a string.
It is text representing a number.
Suppose we tried to perform arithmetic while keeping everything as strings.
For example:
string a = "10";
string b = "20";These are not numeric variables.
To perform calculations, we need actual numeric data types such as:
int
long
doubleThis is where Parse() comes in.
Step 4: What does long.Parse() mean?
long.Parse() converts a string representation of a number into a long.
Example:
string text = "100";
long number = long.Parse(text);Before parsing:
"100"Data type:
stringAfter parsing:
100Data type:
longSo when we write:
long H = long.Parse(input[0]);we are saying:
Take the text stored ininput[0]and convert it into alongnumber.
The Complete Flow
Suppose the input is:
100 20 30The complete process looks like this:
100 20 30
↓
Console.ReadLine()
↓
"100 20 30"
↓
.Split()
↓
["100", "20", "30"]
↓
long.Parse()
↓
100 20 30
↓
H X YAnd the C# code becomes:
string[] input = Console.ReadLine().Split();
long H = long.Parse(input[0]);
long X = long.Parse(input[1]);
long Y = long.Parse(input[2]);Now H, X, and Y are proper numeric variables.
Can we declare H, X, and Y directly?
Yes.
You absolutely can declare them directly:
long H;
long X;
long Y;The problem is not declaring the variables.
The problem is reading three separate values from one input line.
For example:
100 20 30Console.ReadLine() gives us the whole line at once.
So .Split() is used only to separate those values.
The variables are still declared normally.
int.Parse() vs long.Parse()
Both perform the same basic job: converting strings into numbers.
The difference is the numeric type.
Using int.Parse()
int x = int.Parse("100");Use int when the values are relatively small.
Using long.Parse()
long x = long.Parse("10000000000");Use long when values can become much larger.
This is especially important in DSA and competitive programming.
For example, suppose:
N ≤ 10^9An int may be enough.
But if calculations involve multiplication:
10^9 × 10^9the result is:
10^18That will not fit inside a normal C# int.
Using long is safer in such cases.
What happens if the input is invalid?
Consider:
long number = long.Parse("hello");C# cannot convert "hello" into a number.
So this throws an exception.
For user-facing applications, a safer option is TryParse():
if (long.TryParse("100", out long number))
{
Console.WriteLine(number);
}However, in competitive programming, input is normally guaranteed to follow the required format.
So using:
long.Parse()is perfectly common.
Reading Multiple Values in Competitive Programming
A very common pattern is:
string[] input = Console.ReadLine().Split();
int a = int.Parse(input[0]);
int b = int.Parse(input[1]);For three values:
string[] input = Console.ReadLine().Split();
long H = long.Parse(input[0]);
long X = long.Parse(input[1]);
long Y = long.Parse(input[2]);For test cases:
int t = int.Parse(Console.ReadLine());
while (t-- > 0)
{
string[] input = Console.ReadLine().Split();
long H = long.Parse(input[0]);
long X = long.Parse(input[1]);
long Y = long.Parse(input[2]);
Console.WriteLine(H + X + Y);
}A Shorter Way
Once you're comfortable with the basic version, you may see code like:
long[] values = Array.ConvertAll(
Console.ReadLine().Split(),
long.Parse
);Then:
long H = values[0];
long X = values[1];
long Y = values[2];This works, but for beginners I recommend starting with:
string[] input = Console.ReadLine().Split();
long H = long.Parse(input[0]);
long X = long.Parse(input[1]);
long Y = long.Parse(input[2]);It makes each step easy to understand.
C++ vs C# Input Comparison
In C++:
long long H, X, Y;
cin >> H >> X >> Y;In C#:
string[] input = Console.ReadLine().Split();
long H = long.Parse(input[0]);
long X = long.Parse(input[1]);
long Y = long.Parse(input[2]);C++ handles tokenization and conversion for you through cin.
In C#, we commonly perform those steps explicitly.
The 4-Step Rule
Whenever you get confused about competitive-programming input in C#, remember:
READ → SPLIT → PARSE → USE1. Read
Console.ReadLine()Result:
"10 20 30"2. Split
.Split()Result:
["10", "20", "30"]3. Parse
long.Parse()Result:
10, 20, 304. Use
if (X <= Y)
{
Console.WriteLine(0);
}Now you can perform calculations, comparisons, loops, and other operations normally.
Final Example
using System;
public class Test
{
public static void Main()
{
int t = int.Parse(Console.ReadLine());
while (t-- > 0)
{
string[] input = Console.ReadLine().Split();
long H = long.Parse(input[0]);
long X = long.Parse(input[1]);
long Y = long.Parse(input[2]);
if (X <= Y)
{
Console.WriteLine(0);
}
else
{
Console.WriteLine(1);
}
}
}
}Final Takeaway
The important idea is not memorizing the syntax.
Understand what happens to the data.
Input:
10 20 30
↓ Console.ReadLine()
"10 20 30"
↓ Split()
["10", "20", "30"]
↓ Parse()
10, 20, 30
↓ Store
H = 10
X = 20
Y = 30Once this flow becomes clear, C# input handling becomes much easier.
So whenever you see:
long H = long.Parse(input[0]);read it in plain English as:
"Take the first piece of input text, convert it into a long number, and store it in H."
That simple understanding will help a lot when you start solving more DSA problems in C#.