HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Asks the user for an input, stores and prints it to the console c#

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thestoresuserprintsinputforandconsoleasks

Problem

My code is very simple, it asks for an input then stores the input. I am new to c# and wanted to know if it was possible to both ask and accept the input on one line.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    class Program
    {
        static void Main()
        {
            var input = GetInput();
            Console.WriteLine("Your input:\n{0}", input);

        }
        static string GetInput() 
        {
            Console.WriteLine("Please input somthing");
            var input = Console.ReadLine();
            return input;    

        }
    }
}


All feedback welcome!

Solution

Yep, that's totally fine.

Just in case you weren't aware, you might want to add Console.ReadLine(); to the end of your Main() function so that the window stays open until you press enter again. Otherwise the program will print "Your input..." to the screen, reach the end of the Main() function, see that there is nothing else for it to do and exit before you have even had a chance to look at what it printed!

Context

StackExchange Code Review Q#134954, answer score: 3

Revisions (0)

No revisions yet.