Open E-School

Male
Friday, February 24, 1984

Console.ReadLine() - Reading string input from console using C#

Category: Programming Date: Sunday, March 22, 2015
OpenESchool Emagazine

Goal : How to read the string input from console using C#

Namespaces Used :

System ( Console class is defined in System namespace )

Classes used:

Console

Methods used

Console.ReadLine() – Static method defined in the Console class

Brief description

The Console.ReadLine() method takes no arguments and returns the string entered by the user on the console window. It will read the characters from the screen till the user presses the enter key. In other words the Console.ReadLine() methods only reads one string at a time from the console window.

Code

using System;  using System.Data.SqlClient;    namespace DBApplication  {      class Program      {          public static void Main(string[] args)          {              string userInput = "";                Console.Write("Please enter your website : ");              userInput = Console.ReadLine();                Console.WriteLine("Website entered by you is : " + userInput);          }      }  }

Output

Console.ReadLine() method in C#
 

Explanation of the code

In the first line we have created a string variable userInput. We will read the string entered by the user in this variable. In the second line we are directing the user to enter his website name. The Console.ReadLine() method in the third line will wait for the user to enter the string from his keyboard. It will keep waiting till the user presses the enter key. One the user enters the website name and presses enter the ReadLine() method will return the input from the user and we will store it in the userInput string variable. In the last line we are printing back the website name entered by the user.

Remarks

Console.ReadLine() method is significantly different from Console.Read() method. The first method reads the actual string entered by the user whereas the second method returns the ASCII value of the key pressed by the user.

Rate this article and help us improve

Please Login to rate
Overall ratings: 0 | Rating: out of 5
Previous Article Next Article

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp