Reading and Writing to a Console - Chapter- 2

In this chapter we will learn.

1- Reading from console
2- Writing to the console
3- 2 ways to write to console
   a) Concatenation
   b) Place holder syntax - Most preferred


Sample Program

using System;
class Program
    {
        static void Main()
        {
            //Write the user for his name
            Console.WriteLine("Please enter your name");
            //Read the name from console
            string UsreName = Console.ReadLine();
            //Concatenate name with hello word and print
            Console.WriteLine("Hello"+UsreName);
            //Placeholder syntax to print name with hello word
            Console.WriteLine("Hello{0}",UsreName);
        }
    }

In chapter 1 introduction to c# we have seen following.

1- Basic structure of a C# program.
2- What is a Namespace?
3- Purpose of Main Method.

We have seen a very simple program in chapter 1.

//Namespace Declaration.
using System;
    class Program
    {
        static void Main()
        {
            //Write to console.
            Console.WriteLine("Welcome to Vepsh Technology");
        }
    }

But in chapter 2 we are gonna make this program little more interactive. 

For example when we run this program it should prompt to user to enter their name. So when we run this program it should ask to user please enter your name and the user type in his name for example let say “Vepsh” and our program should output “Hello Vepsh”. So this program should be little interactive. It should take the input from the use, who is running this program and print that name back to the console. So reading from the console and writing back to the console that’s what our program should do.

Let’s go and create a demo console application for reading and writing to a console.

>> Now look at the demo.

So, what should our program do it should print a messes to the console asking the user to enter their name. And in the previous example we have seen to write a message on to the screen we can use the WriteLine() method that is present in the console class. And we know that console class is present inside the System namespace.

For example:

Console.WriteLine("Please enter your name.");

So, what happen when we will run this program? It will prompt the user to enter their name. Once they enter their name and presses enter we should read that name from the console.

For example if we run this program so it will print following message on the console.

Reading and Writing to a Console

Now user has to type their name on console window and we should read that name form the console. And to do that to read something from the console we can use the ReadLine() method that is also present in the console class.

For example:-

Console.ReadLine();

So what will this function do, this function will read a line that has been type on the console by the user who is interacting with program. If he types for example ‘Vepsh’ and presses enter this ReadLine() method will  read that word ‘Vepsh’  from the console.

Note: - ReadLine() Method Reads a line of characters from the current string and returns the data as a string.

The ReadLine() returns a string . So ReadLine () method is going to give that name back to you. So you have to store that in a variable so you can use it latter for printing. So I am using a string variable called ‘UserName’.

For example:-

string UsreName = Console.ReadLine();

Now if user enters ‘Vepsh’ this method is going to read that and give it to this string variable. And this variable holds that value. So what we want to do with that we want to print it on to the console. And to do that again write something on to the console we can use WriteLine() method.

But this time if user typed ‘Vepsh’ we want print message ‘Hello Vepsh’.

For example:-

Console.WriteLine("Hello "+ UsreName);

Now go a head and run this program.

Reading and Writing to a Console

Reading and Writing to a Console

Actually to write to the console there are two different ways.
1- Concatenation 
//Concatenate name with hello word and print
Console.WriteLine("Hello "+ UsreName);
2- Place holder syntax - Most preferred 
//Placeholder syntax to print name with hello word
Console.WriteLine("Hello{0}", UsreName);

>> Now look at the demo program using place holder syntax.

using System;
namespace ReadingAndWritingToAConsole
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Please enter your first name");
            string FirstName = Console.ReadLine();
            Console.WriteLine("Please enter your last name");
            string LastName = Console.ReadLine();
            Console.WriteLine("Hello {0},{1}", FirstName, LastName);
            Console.ReadLine();
        }
    }
}

Now run the program.

Reading and Writing to a Console

Note: - It is worth to note the following points:
  • C# is case sensitive.
  • All statements and expression must end with a semicolon (;).
  • The program execution starts at the Main method.
  • Unlike Java, program file name could be different from the class name.