TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Console.ReadLine Tips

This C# example program uses the Console.ReadLine method. It reads in user input as strings.

Console.ReadLine reads input from the console.

When the user presses enter, it returns a string. We then resume the program and process the string to determine the next action to take.

Example. First, when developing testing programs you will often want to loop through user inputs. This example shows how to use a "while (true)" loop and the Console.ReadLine method to do this.

While

Next: The example shows how to test the string value of the result. It shows how to access the Length property of that string.

String Length

C# program that uses Console.ReadLine loop

using System;

class Program
{
    static void Main()
    {
	while (true) // Loop indefinitely
	{
	    Console.WriteLine("Enter input:"); // Prompt
	    string line = Console.ReadLine(); // Get string from user
	    if (line == "exit") // Check string
	    {
		break;
	    }
	    Console.Write("You typed "); // Report output
	    Console.Write(line.Length);
	    Console.WriteLine(" character(s)");
	}
    }
}

Output

Enter input:
Dot
You typed 3 character(s)
Enter input:
Net Perls
You typed 9 character(s)
Enter input:

In Main, the program loops infinitely while prompting the user in each iteration. The program shows the length property access on the string type for each string typed. If the user types "exit", then the program immediately terminates.

Example 2. Here we use the string from Console.ReadLine as an integer value. You can invoke the int.TryParse method to see if the result string is an integer representation, and it will return the value of that integer if possible.

Next: This program tries to multiply an integer value received by the user by 10 and display the product.

Multiply

C# program that parses Console input

using System;

class Program
{
    static void Main()
    {
	Console.WriteLine("Type an integer:");
	string line = Console.ReadLine(); // Read string from console
	int value;
	if (int.TryParse(line, out value)) // Try to parse the string as an integer
	{
	    Console.Write("Multiply integer by 10: ");
	    Console.WriteLine(value * 10); // Multiply the integer and display it
	}
	else
	{
	    Console.WriteLine("Not an integer!");
	}
    }
}

Output

Type an integer:
4356
Multiply integer by 10: 43560

The program prompts the user for an input. The string variable with the identifier "line" is then assigned to the reference of the string data allocated by Console.ReadLine and filled with the user's input.

Then: The int.TryParse static method tests for a numeric value, and if this test succeeds we can then use the integer.

ParseStatic Method

Finally. You can insert a Console.ReadLine method call at the end of the Main method—or even in a finally block in the Main method. This will ensure the terminal window is never dismissed by Windows immediately on program completion.

FinallyMain Args Examples

 

Summary. We looked at the Console.ReadLine method. We used it to accept input in a loop and testing the result each time. Second, we parsed the result of Console.ReadLine into an integer type for numerical processing.

Note: The Console.ReadLine method is useful in many console programs and is fairly simple to use.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf