days
0
-18
0
hours
-1
-8
minutes
-1
-5
seconds
-1
-5
search
C# tutorial

How to read and write in Console app in C#

Vikas lalwani
C#
© Shutterstock / panitanphoto

Newcomers to learning the C# programming language might have some questions about how they can read or write to Console app. This tutorial by full-stack developer Vikas lalwani will show you a few examples, some sample code, and some output. By the end you will know a few new methods that will help you out as you continue to learn how to program with C#.

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 App

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.

SEE ALSO: Programming with Beef: Open source language takes inspiration from C#

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  to exit... ");
            while (Console.ReadKey().Key != ConsoleKey.Enter)
            {
                //run loop until Enter is press
            }
        }
    }
}

Output:

In the above example, 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 a new line, then using Console.Write() to print “Press <Enter> to exit…” which keeps the cursor in the 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 in Console App

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, the difference between Console.ReadLine() and Console.Read() 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 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:

C#

In the above example, we are using both the methods, Console.Read() and Console.ReadLine().

SEE ALSO: 2020 IT skills: what we learned from 213,782 coding tests

As you can see, initially, we are asking the user to enter a single character, this character value is stored variable “character” and then used to print the value using Console.WriteLine().

In the second part of the program, we are trying to print a sequence of lines, until the user clicks “Ctrl+Z”, using do-while loop. Inside the do-while loop we are getting each line entered by the user using Console.Readline and assigning that value to “line”, which is then print using Console.WriteLine(), we are also checking if the “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.

Author
I am full-stack web-developer and owner at Colossal Techno, IT Services company. I mostly work on technologies which include HTML, CSS, jQuery, Javascript, Bootstrap, C#, Xamarin.Forms, ASP.NET MVC/Core and SQL server. In my free time I like to browse more programming related articles and learn something new.

Leave a Reply

avatar
400