Search code examples
c#console.writelineconsole.readline

c# Console.WriteLine();


In C#, after I write Console.WriteLine() and I'm asked to enter several values, can I get them all in one method? For example:

double a, b, c = 0;
Console.WriteLine("please enter the values of:\n a value:\n b value: \n c value:");

thanks for the help (:


Solution

  • There's no BCL methods for this specific functionality, but you could use a helper function to collect these without too much repetition.

    static void Main(string[] args)
    {
        string RequestInput(string variableName)
        {
            Console.WriteLine($"{variableName}:");
            return Console.ReadLine();
        }
    
        Console.WriteLine("please enter the values of:");
        var a = double.Parse(RequestInput("a"));
        var b = double.Parse(RequestInput("b"));
        var c = double.Parse(RequestInput("c"));
    
    }