What is the Console Application? Reading and Writing to Console

Before we start talking about how to read from and writing to console application, first understand what is a console application.

Console Application

A console is an application that is designed to operate with text only. It does not have any graphical user interface, does not take any input in the input box, and does not show you output in any label. It simply has a black screen and shows some very important information on the screen such as, at what part the hard drive the user is working at this moment. It shows the name of the working drive and directory. If you design a console application you can design according to your need.

Why Console

We use console applications for speed and simplicity. Because the console application has no heavy graphics, there is almost no load on the processor. The computer will run faster and will run your application faster too because it has no burden to process the graphics of your application. The best part, you need just a keyboard to work with a console application.

The concept of a console application is so good that every operating system has a console application as an interface. Such as Windows has Command Prompt, Linux, and Macintosh have a terminal.

The console applications have the ability to process data in the background with no graphical data processing in the front end. This is why these applications are extremely optimized. Due to there optimized property, big organizations still prefer console interfaces for their core data processing applications.

How to Read from Console Application

As we have already understood in our last article, Console is a class that is located inside the System directory or System namespace, the system namespace is provided by .Net Framework.

The console class has two methods to read from the console.

  1. ReadLine()
  2. Read()

ReadLine()

Let’s talk about ReadLine First. We should write ReadLine as “ReadLine()” because it is a method and in programming, a method is written with () in the end.

Once you write the following line in your program and run it, a cursor will start blinking on a black screen which means on Console application your program is asking for input.

Console.ReadLine();

ReadLine() starts reading from the next character and keeps on reading all characters until you press the Enter button. Once the enter button is pressed it takes all the characters converts them into a string and accepts the input as String.

Important Note – ReadLine() takes input as a string and provides output as a string.

ReadLine() program

using System;
using System.Collections.Generic;


namespace Namespace
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter your Name - ");
            String name = Console.ReadLine();
            Console.WriteLine("Welcome Mr. {0}", name);
            Console.ReadLine();
        }
    }
}

Read()

This method is also used to read from the console panel but it reads only one character and returns its ASCII code. So the data type that it returns is Int unlike ReadLine() that returns a String.

If you are using Read() method and you type 10 characters and press enter. It will only take the first character that you types and ignore the rest. Let us see an example program below. The program and input are exactly the same as the Readline program above but the change is only Read().

using System;
using System.Collections.Generic;


namespace Namespace
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter your Name - ");
            int name = Console.Read();
            Console.WriteLine("Welcome Mr. {0}", name);
            Console.ReadLine();
        }
    }
}
Read output with Capital case latter
Read output with string
Read output with lower case latter
Read output with lower case string
Read output with Numeric

Explaination

Looking at the output of the above applications you might have understood, it does not even matter how many characters you are entering when using Read() method, it will only accept one character. When you type “Brad” it took the ASCII value of Capital letter B which is 66.

When you types “brad” it took the ASCII value of latter b which is 98.

It does not even care if you are tying a string or an int. It cares about a character and returns its ASCII code. Speaking of ASCII character, in case you are interested to know about ASCII character then visit this page.

Important Note – Read() takes input as a string and as an int but provides output as an int only.

How to Write to the Console

Just like ReadLine() and Read(), the Console has two methods for writing something to the console.

  1. WriteLine()
  2. Write()

Differece between WriteLine() and Write()

The WriteLine method is used to write something to the console application and Adds enter at the very end. It means when you write something to the console using WriteLine, nothing can be started just after the WriteLine statement because enter is added at the very end of it and a new line is selected.

Do not worry if you do not understand anything at this point because things will be crystal clear with an example below.

using System;

namespace Namespace
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Brad ");
            Console.WriteLine("is a");
            Console.Write("Nice");
            Console.WriteLine("Programmer");
            Console.ReadLine();
        }
    }
}
WriteLine and Write Output

Explaination

As you can see in the output of the program, every time the WriteLine method is used, the next statement starts with a new line. Only once we have used the Write method in the above program when we printed Nice and you can see the next line Programmer starts just after where the Nice Ends. There is not even a single gap. Because the Write method does not add Enter in the very end that is why the next line starts where the Write statement ends.

I hope you have understood the difference. If you have any confusion, feel free to comment.

Take Care Guys and Good Luck!