C#:C# Stdin Input: Reading Lines Until EOF with Console.ReadLine()
C#•Stdin Parsing

C# Stdin Input: Reading Lines Until EOF with Console.ReadLine()

Compilra runs C# inside Mono 6.6 (supporting C# 7.0 with synchronous public static void Main()). Supply all input lines in the Stdin panel before clicking Run and iterate with while ((line = Console.ReadLine()) != null) until end-of-stream.

Syntax

while ((line = Console.ReadLine()) != null) { ... }

Parameters & Values

Key components and acceptable values for this structure:

ParameterTypeDescription
Console.ReadLine()stringReads the next line from stdin and returns null once end-of-input (EOF) is reached.
CultureInfo.InvariantCultureIFormatProviderEnsures "." decimal points parse consistently regardless of OS locale settings.
decimal.Parse()decimalParses monetary or exact base-10 numeric strings without binary floating-point rounding drift.

Ready-to-Run Code Example

Executable in Compilra

Run this complete, working example directly without installing C# locally:

using System;
using System.Globalization;

public class Program {
    public static void Main() {
        decimal grandTotal = 0m;
        int idx = 0;
        string line;

        while ((line = Console.ReadLine()) != null) {
            line = line.Trim();
            if (line.Length == 0) continue;

            idx++;
            string[] parts = line.Split(':');
            string orderId = parts[0].Trim();
            string[] prices = parts[1].Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            decimal orderSum = 0m;
            foreach (string p in prices) {
                orderSum += decimal.Parse(p, CultureInfo.InvariantCulture);
            }

            grandTotal += orderSum;
            Console.WriteLine($"#{idx} {orderId}: {orderSum.ToString("F2", CultureInfo.InvariantCulture)}");
        }

        Console.WriteLine($"Grand Total: {grandTotal.ToString("F2", CultureInfo.InvariantCulture)}");
    }
}
Pre-Supplied Stdin (Input):
Order-101: 12.50 26.50
Order-102: 45.00
Order-103: 19.25 29.75
Expected Output:
#1 Order-101: 39.00
#2 Order-102: 45.00
#3 Order-103: 49.00
Grand Total: 133.00

How It Works: Step-by-Step

  • Lines 5-12: Uses a synchronous Main() entry point and loops Console.ReadLine() until null (EOF), skipping blank lines.
  • Lines 14-21: Splits each order record on ":" and parses individual space-separated prices with decimal.Parse.
  • Lines 23-28: Formats per-order totals and the final grand total using two decimal places.

Key Concepts

  • Compilra sends your pre-supplied Stdin box before launching Mono 6.6; input cannot be typed interactively once execution starts.
  • Mono 6.6 requires synchronous public static void Main() (not C# 7.1 async Task Main() or C# 9+ top-level statements).
  • Skipping empty lines inside the while loop makes your parser resilient to trailing newlines at the end of stdin.

Common Mistake: Using C# 9+ top-level statements or assuming Console.ReadLine() never returns null

Top-level statements fail to compile under Mono 6.6, and passing null to decimal.Parse() throws ArgumentNullException at EOF.

❌ Incorrect
decimal val = decimal.Parse(Console.ReadLine()); // Throws on EOF null!
✅ Correct
string line;
while ((line = Console.ReadLine()) != null) {
    if (line.Length > 0) { /* Safe parse */ }
}

Related C# Tutorials & Compiler

Run and test C# on Compilra

Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.

C# Online Compiler