You can also prompt the user to enter data and then use the Console.ReadLine method to assign the input to a string variable. The following example asks the user for a name and then display a message that includes the input:
The Console.ReadLine method waits for user input and then assigns it to the variable. The next statemtnt displays a formatted string containing hello with the user input. For example, if you enter David, the output will be Hello David.
Note : the empety parentheses in the ReadLine method. This means that it does not take any arguments.
USER INPUT
USER INPUT
The Console.ReadLine() method returns a string value. If you are expecting another type of value ( such as int or double ), the entered data must be converted to that type. This can be done using the Convert.ToXXX methods, where XXX is the .NET name of the type that we want to convert to. For example, methods include Convert.ToDouble and Convert.ToBoolean.
For integer conversion, there are three alternatives available based on the bit size of the integer:
- Convert.ToInt16
- Convert.ToInt32
- Convert.ToInt64
The default int type in C# is 32-bit. Let's create a program that takes an integer as input and displays it in a message:
if, in the program above, a non-integer value is entered (for example, letters), the Convert will fail and cause an error.
Next : C# series 1.8 : Var Keyword
Next : C# series 1.8 : Var Keyword
No comments:
Post a Comment