How to input multiple values from user in one line in C#?

In C#, you can input multiple values from the user in one line using several approaches. The most common method is using Console.ReadLine() with Split() to parse space-separated or comma-separated values into an array.

Syntax

Following is the syntax for reading multiple values in one line using Split()

string input = Console.ReadLine();
string[] values = input.Split(' '); // or Split(',') for comma-separated

For converting to integers −

int[] numbers = input.Split(' ').Select(int.Parse).ToArray();

Using Split() with Space Separator

This approach reads a single line of space-separated values and converts them into an array −

using System;
using System.Linq;

class Program {
    static void Main() {
        Console.Write("Enter multiple numbers separated by spaces: ");
        
        // Simulate user input: "10 20 30 40 50"
        string simulatedInput = "10 20 30 40 50";
        Console.WriteLine(simulatedInput);
        
        int[] numbers = simulatedInput.Split(' ').Select(int.Parse).ToArray();
        
        Console.WriteLine("You entered:");
        foreach (int num in numbers) {
            Console.Write(num + " ");
        }
        Console.WriteLine();
        Console.WriteLine("Total numbers: " + numbers.Length);
    }
}

The output of the above code is −

Enter multiple numbers separated by spaces: 10 20 30 40 50
You entered:
10 20 30 40 50 
Total numbers: 5

Using Split() with Comma Separator

For comma-separated values, you can modify the separator −

using System;
using System.Linq;

class Program {
    static void Main() {
        Console.Write("Enter numbers separated by commas: ");
        
        // Simulate user input: "15,25,35,45"
        string simulatedInput = "15,25,35,45";
        Console.WriteLine(simulatedInput);
        
        int[] numbers = simulatedInput.Split(',').Select(int.Parse).ToArray();
        
        Console.WriteLine("Parsed numbers:");
        for (int i = 0; i < numbers.Length; i++) {
            Console.WriteLine($"Number {i + 1}: {numbers[i]}");
        }
        
        int sum = numbers.Sum();
        Console.WriteLine("Sum: " + sum);
    }
}

The output of the above code is −

Enter numbers separated by commas: 15,25,35,45
Parsed numbers:
Number 1: 15
Number 2: 25
Number 3: 35
Number 4: 45
Sum: 120

Using Multiple Values for Matrix Input

You can input an entire row of a matrix in one line −

using System;
using System.Linq;

class Program {
    static void Main() {
        int[,] matrix = new int[2, 3];
        string[] simulatedRows = {"1 2 3", "4 5 6"};
        
        Console.WriteLine("Enter matrix elements (3 numbers per row):");
        
        for (int i = 0; i < 2; i++) {
            Console.Write($"Row {i + 1}: ");
            Console.WriteLine(simulatedRows[i]);
            
            int[] rowValues = simulatedRows[i].Split(' ').Select(int.Parse).ToArray();
            
            for (int j = 0; j < 3; j++) {
                matrix[i, j] = rowValues[j];
            }
        }
        
        Console.WriteLine("\nMatrix:");
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                Console.Write(matrix[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
}

The output of the above code is −

Enter matrix elements (3 numbers per row):
Row 1: 1 2 3
Row 2: 4 5 6

Matrix:
1 2 3 
4 5 6 

Comparison of Input Methods

Method Use Case Example Input
Split(' ') Space-separated values "10 20 30"
Split(',') Comma-separated values "10,20,30"
Split() with multiple chars Mixed separators "10,20 30;40"

Conclusion

Using Console.ReadLine().Split() combined with Select() and ToArray() is the most efficient way to input multiple values from the user in one line in C#. This approach works well for both simple arrays and more complex data structures like matrices.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements