Reading And Writing To Console

Reading And Writing To Console

In the previous tutorial of this C# series, We learnt how to print data to Console and we learnt why we use Main method and briefly about namespaces and classes

In this tutorial, We are going to write an interactive program which takes input from the end user and Write to Console. We can write to the Console either by concatenation or by using place holder syntax.

Reading From Console

There is a predefined method in Console class under System namespace to read the contents from the console(From end user) and store it in some variable etc. This method returns the data which it read from the end user in the format of a string and its name is ReadLine() method. As it is under the Console class, we have to write it as Console.ReadLine() method.

eg: string name = Console.ReadLine();
This ReadLine Method Takes the input what the user has given and stores it under a variable called name.

Writing To Console

As we have discussed in the previous tutorial, the method is WriteLine() method.It also comes under the console class.So, we have to use it as Console.WriteLine() method.

eg: Console.WriteLine(“I am a Coder”)
This WriteLine Method Writes “I am a Coder” to the Console.

Ways to write to Console


If you are familiar with other object oriented languages like python,java etc.. then it will be very easy to understand this concept. Concatenation is nothing but placing strings side by side. We can print to console using this concatenation.

eg: “Hello” +” learner” = “Hello learner”
“Hi ” +”9″ = “Hi9”

Here we are placing quotes around every word because they are strings. strings should be placed in double quotes. If you want
your program to be more flexible then you can store in a variable.

eg: string name =” Jack”
string frend =” John”

“Hi “+name = “Hi Jack”
name + frend = “Jack John”

Here we stored the string(anything inside a double quote) in a variable called name and concatenated with “Hi ” which renders Hi Jack as output. As we are already storing a double quote word inside a string, so, there is no need to keep the string inside a double quote.

Place holder syntax

Place holder syntax is very flexible when compare to the concatenation Method.Here in this Place holder method, we will write {0},{1},{2}…etc in the places of variables and write variable names at the end.

eg: Console.WriteLine(“My name is {0}”, “jack”);
Here, in the above example the string “jack” replaces the {0}
and writes as “My name is jack” to the console

eg: Console.WriteLine(“My name is {0} and frend is {1}”, “Jack”,”John”)
If we have more items to write to the console, the more numbers we need to write. Here “Jack” replaces {0} and “John” replaces {1} and writes as “My name is Jack and frend is John” to the console.

eg: string name =”Jack”;
Console.WriteLine(“My name is {0}”,name)

As we discussed above,if we are using a variable then there is no need to write in double quotes. The above eg writes My name is Jack to the console. Here, the variable name repalces the {0}.


Here, in the above program we have imported system namespace, which is necessary to read and write from console and later we declared a class named MyProgram and we have written Main method which is the default entry of the above Program.
We stored the first input from the user in a variable called name.Similarly, the second input in age and third in country variables.Here, we used two ways to
write the output in desired way to the console one is concatenation method and another is place holder method.

Leave a Reply

Your email address will not be published. Required fields are marked *