We’ll be covering the following topics in this tutorial:
Console Input
The Console class allows using the Write () and the WriteLine () functions to display things on the screen. While the Console.Write () method is used to display something on the screen, the Console class provides the Read () method to get a value from the user. To use it, the name of a variable can be assigned to it. The syntax used is:
variableName = Console. Read();
This simply means that, when the user types something and presses Enter, what the user had typed would be given (the word is assigned) to the variable specified on the left side of the assignment operator.
Read() doesn’t always have to assign its value to a variable. For example, it can be used on its own line, which simply means that the user is expected to type something but the value typed by the user would not be used for any significant purpose. For example some versions of C# (even including Microsoft’s C# and Borland C#Builder) would display the DOS window briefly and disappear. You can use the Read() function to wait for the user to press any key in order to close the DOS window.
Besides Read(), the Console class also provides the ReadLine() method. Like the WriteLine() member function, after performing its assignment, the ReadLine() method sends the caret to the next line. Otherwise, it plays the same role as the Read() function.
string FirstName;
Console.write(“Enter First Name: “);
FirstName = console.ReadLine();
In C#, everything the user types is a string and the compiler would hardly analyze it without your explicit asking it to do so. Therefore, if you want to get a number from the user, first request a string. after getting the string, you must convert it to a number. To perform this conversion, each data type of the .NET Framework provides a mechanism called Parse. To use Parse(), type the data type, followed by a period, followed by Parse, and followed by parentheses. In the parentheses of Parse, type the string that you requested from the user.
Here is an example:
using system;
namespace GeorgetownCleaningservices
{
class orderprocessing
{
static void Main()
{
int Number;
string strNumber;
strNumber = console.ReadLine();
Number = int.parse(strNumber);
}
}
}
Console Output
Instead of using two Write() or a combination of Write() and WriteLine() to display data, you can convert a value to a string and display it directly. To do this, you can provide two strings to the Write() or WriteLine() and separate them with a comma:
1. The first part of the string provided to Write() or WriteLine() is the complete string that would display to the user. This first string itself can be made of different sections:
(a) One section is a string in any way you want it to display
(b) Another section is a number included between an opening curly bracket “{“and a closing curly bracket “}”.This combination of”{” and “}”is referred to as a, placeholder.
You can put the placeholder anywhere inside of the string. The first placeholder must have number O. The second must have number 1, etc. With this technique, you can create the string anyway you like and use the placeholders anywhere inside of the string
2. The second part of the string provided to Write() or WriteLine() is the value that you want to display. It can be one value if you used only one placeholder with 0 in the first string. It’ you used different placeholders, you can then provide a different value for each one of them in this second part, separating the values with a comma
Here are examples:
Demonstration of Writeline method
using System;
// An Exercise class
class Exercise
{
static void Main()
{
String FullName = “Anselme Bogos”;
int Age = 15;
double Hsalary = 22.74;
Console. writeline (“Full Name: {0}”, Full Name) ;
console.writeline(“Age: {0}”, Age);
console.writeline(“Distance: {0}”, Hsalary);
console.writeline(“Name: {0}\n Age: {1}\n Distance:{2}”,
FullName, Age, Hsalary);
Console.writeline();
}
}
OUTPUT:
Full Name: Anselme Bogos
Age: 15
Distance: 22.74
Full Name: Anselme Bogos
Age: 15
Distance: 22.74
As mentioned already, the numeric value typed in the curly brackets of the first part is an ordered number. If you want to display more than one value, provide each incremental value in its curly brackets. The syntax used is:
write(“To Display {0} {l} {2} {n}, First, second, Third, nth);
You can use the sections between a closing curly bracket and an opening curly bracket to create a meaningful sentence.
The System names pace provides a specific letter that you can use in the Write() or WriteLine()’s placeholder for each category of data to display. To format a value, in the placeholder of the variable or value, after the number, type a colon and one of the appropriate letter from the following table. If you are using ToStringO, then, in the parentheses of
ToString(), you can include a specific letter or combination inside of double-quotes. The letters and their meanings are:
Use of particular character in writeline method
using System;
// An Exercise class
class Exercise
{
static void Main()
{
double Distance = 248.38782;
int Age = 15;
int Newcolor = 3478;
double Hsal = 22.74, Hrswork = 35.5018473;
double weeklysal = Hsal * Hrswork;
Console.writeLine(“Distance: {OJ”, Distance.ToString(“E”));
console.writeLine(“Age: {O}”, Age.ToStringO);
consol e.writeLine(“color: {O}”, Newcolor .Tostring(“x”));
Console.writeLine(“weekly salary: {OJ for .{1} hours”,
weeksal.ToString(“c”), Hrswork.ToString(“F”));
console.writeLine();
}
}
OUTPUT:
Distance: 2.483878E+002
Age: 15
color: D96
weekly salary: $807.31 for 35.50 hours
To specify the amount of space used to display a string, you can use its placeholder in Write() or WriteLine(). To do this, in the placeholder, type the 0 or the incrementing number of the placer and its formatting character if necessary and if any. Then, type a comma followed by the number of characters equivalent to the desired width. Here are examples:
Demonstration of Alinement in Writeline method
using System;
// An Exercise class
class Exercise
{
static void Main()
{
String Full Name = “Anselme 80gos”;
int Age = 15;
double Marks = 22.74;
console.writeline(“Full Name: {O,20}”, Full Name);
Console.writeline(“Age: {O,14}”, Age.ToString());
console.writeline(“Marks:{O:C,8}”, Marks.ToString());
Console. writeline(“——————————“) ;
Console.writeline();
}
}
OUTPUT:
Full Name: Anselme bogos
Age: 15
Marks: $22.74
The sign you provide for the width is very important. If it is positive, the line of text is aligned to the right. This should be your preferred alignment for numeric values. If the number is negative, then the text is aligned to the left.