We can use the Console class to read values.
using System; public class ReadLine { static void Main() { string name; Console.Write("Enter your name: "); name = Console.ReadLine(); Console.WriteLine("Hello {0}", name); } }
The second program will read a value from a console and print it.
string name;
We declare a variable. The variable is called 'name'.
Unlike constants, which store only one value during the life of the program, variables may store various different values of the same type.
The
string
keyword defines the data type of the variable. Our variable will hold string values.name = Console.ReadLine();
We read a line from the terminal. When we hit the Enter key, the input is assigned to the name variable.
Console.WriteLine("Hello {0}", name);
In this code line, we perform variable interpolation.
Variable interpolation is replacing variables with their values inside string literals. Another names for variable interpolation are: variable substitution and variable expansion.
$ ./readline.exe Enter your name: Steve Jobs Hello Steve Jobs
This is the output of the program.
No comments:
Post a Comment