If you have just started learning C#, you might be interested to know, how we can read or write to console app using C#, in this article, I have explained it with an example.
Writing in Console Application using C#
In C# you can write or print to console using Console.WriteLine() or Console.Write(), basically both methods are used to print output of console.
Only difference between Console.WriteLine() and Console.Write() is that, Console.WriteLine()
also makes control move to the next line while Console.Write()
keeps the Control in same line.
Let’s take a look at an example:
using System;
namespace BasicIO
{
public class BasicIOExample
{
public static void Main()
{
DateTime dat = DateTime.Now;
//print current date and time and moves cursor to next line
Console.WriteLine("Current Date and time is : "+dat);
//prints text but keeps cursor in same line
Console.Write("Press <Enter> to exit... ");
while (Console.ReadKey().Key != ConsoleKey.Enter)
{
//run loop until Enter is press
}
}
}
}
Output:
In the above example, we are using both the methods Console.WriteLine(
) and Console.Write()
, when using Console.WriteLine()
, we are printing current date and time and cursor is moved to new line, in next line of code, we are using Console.Write()
to print "Press <Enter> to exit…" which keeps the cursor in same line.
You will also notice that while loop keeps on running, means you will see the output on console, until you click "Enter" on your keyboard as Console.ReadKey().Key
is looking for Console.Enter
Key to end this while
loop.
Reading user input from Console Application in C#
When we want to read user’s data in C# in Console application, we can use Console.Readline() or Console.Read() method of C#.
Basically, difference between Console.ReadLine() and Console.Read() method in C#, is
Console.Read:
Reads the next character from the standard input stream.Console.ReadLine
: Reads the next line of characters from the standard input stream.
You can understand it as, Console.Read() reads a character, so if you are on a console application and you press a key then the console will close, but when using Console.Readline() it will read the whole string.
Let’s understand it with an example
using System;
namespace BasicIO
{
public class BasicIOExample
{
public static void Main()
{
char charcter;
string line;
Console.Write("Enter single character :");
//read character from user input
charcter = Convert.ToChar(Console.Read());
//print it using WriteLine()
Console.WriteLine("Character is: " + charcter);
Console.WriteLine("Enter one or more lines of text (press CTRL+Z to exit):");
//do-while loop to read lines
do
{
//read a complete line
line = Console.ReadLine();
//check if line is empty or not
if (line != null && line != "")
{
//if line has data print
Console.WriteLine("Line was = " + line);
}
} while (line != null);
}
}
}
Output:
In the above example, we are using both the methods, Console.Read() and Console.ReadLine().
As you can see, initially, we are asking user to enter a single character, this character value is stored variable “charcter” and then used to print the value using Console.WriteLine().
In second part of the program, we are trying to print sequence of lines, until the user click “Ctrl+Z”, using do-while loop. Inside do-while loop we are getting each line entered by user using Console.Readline and assigning that value to “line”, which is then print using Console.WriteLine(), we are also checking if “line” variable is not empty or not, we are printing value only if “line” variable is not empty.
You can read the complete C# tutorial here with online sample program, without any need of extra editor.
You may also like to read:
First program in C# using Visual Studio