C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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 LengthMain: Here the code loops infinitely while prompting the user in each iteration.
And: The program shows the length for each string typed. If the user types "exit", then the program immediately terminates.
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:
Next: This program tries to multiply an integer value received by the user by 10 and display the product.
Line: The string "line" is assigned to the reference of the string data allocated by Console.ReadLine and filled with the input.
Then: The int.TryParse static method tests for a numeric value, and if this test succeeds we can then use the integer.
int.ParseStaticC# 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
And: This will ensure the terminal window is never dismissed by Windows immediately on program completion.
Finally