In this tutorial, we will learn how to take input from user and and display output in C# using various methods
In order to output something in C#, we can use
System.Console.WriteLine() OR System.Console.Write()
Here, System is a namespace, Console is a class within namespace System and WriteLine and Write are methods of class Console.If we are using , “using System” then we can simply print like this Console.Write(“Something”);
The main difference between WriteLine() and Write() is that the Write() method only prints the string provided to it, while the WriteLine() method prints the string and moves to the start of next line as well.
Strings can be combined/concatenated using the + operator while printing.
Let’s look at a simple example that prints a string to output screen.
using System; namespace Sample{ class Test{ public static void Main(string[] args){ // Printing strings Console.WriteLine("Interviewbit is cool"); // Printing integers Console.WriteLine(10); // Printing variables int a = 100; Console.WriteLine(a); // Printing Concatenated String using + operator int val = 55; Console.WriteLine("Hello " + "Interviewbit"); Console.WriteLine("Value = " + val); } } }
When we run the program, the output will be
Interviewbit is cool 10 100 Hello Interviewbit Value = 55
In C#, the simplest method to get input from the user is by using the ReadLine() method of the Console class. However, Read() and ReadKey() are also available for getting input from the user. They are also included in Console class.
Read() - The Read() method reads the next character from the standard input stream. It returns the ascii value of the character.
ReadLine() - The ReadLine() method reads the next line of input from the standard input stream. It returns the same string.
ReadKey() - The ReadKey() method obtains the next key pressed by user. This method is usually used to hold the screen until user press a key.
Reading numeric values can be slightly tricky in C#. We’ll still use the same ReadLine() method we used for getting string values. But since the ReadLine() method receives the input as string, it needs to be converted into integer or floating point type.
One simple approach for converting our input is using the methods of Convert class.
Let’s look at a simple example that reads inputs and print it to output screen.
using System; namespace UserInput{ class MyClass{ public static void Main(string[] args){ string userInput; int intVal; double doubleVal;
Console.Write("Enter a string: ");
userInput = Console.ReadLine();
Console.WriteLine("You entered " + userInput ); Console.Write("Enter integer value: "); userInput = Console.ReadLine(); /* Converts to integer type */ intVal = Convert.ToInt32(userInput); Console.WriteLine("You entered " + intVal); Console.Write("Enter double value: "); userInput = Console.ReadLine(); /* Converts to double type */ doubleVal = Convert.ToDouble(userInput); Console.WriteLine("You entered " + doubleVal); } } }
When we run the program, the output will be
Enter a string: Interviewbit You entered Interviewbit Enter integer value: 101 You entered 101 Enter double value: 59.412 You entered 59.412
Try taking a user input and printing space separated “Hello” and input (without quotes) in the editor below.
Input Format
Read a string input.
Output Format
Print Hello input to stdout.
Example Input
InterviewBit
Example Output
Hello InterviewBit