Zia system of Education Lodhran
Visual Programming:
Write() and WriteLine()
The difference between Write () and WriteLine () method is based on new
line character.
Write() method displays the output but do not provide a new line character.
WriteLine() method displays the output and also provides a new line character it
the end of the string, This would set a new line for the next output.
Let us see an example to learn about the difference between Write() and
WriteLine() method −
Example
using System;
class Program {
static void Main() {
Console.Write("One");
Console.Write("Two");
// this will set a new line for the next output
Console.WriteLine("Three");
Console.WriteLine("Four");
Console.WriteLine("Five");
}
Output
OneTwoThree
Four
Five
Uses of C# Readline Method
The C# readline method is mainly used to read the complete string until the user
presses the Enter key or a newline character is found.
Using this method, each line from the standard data input stream can be read. It is also
used to pause the console so that the user can take a look at the output.
Type-Casting for Non-string Type Inputs for C# Readline
Method
The default data type of the C# readline is string. Some of the typecasting methods for
non-string input data types in order to convert them from string to the respective data
types are as given below-
int data type
Method - Convert.ToInt32();
Syntax - Convert.ToInt32(Console.ReadLine());
double data type
Method - Convert.ToDouble();
Syntax - Convert.ToDouble(Console.ReadLine());
boolean data type
Method - Convert.ToBoolean();
Syntax - Convert.ToBoolean(Console.ReadLine());
char data type
Method - Convert.ToChar();
Syntax - Convert.ToChar(Console.ReadLine());
using System;
using System.IO; // defining the System package
class Program1 {
public static void Main(string[] args) // defining the main function
string feeling; //declaring variable feeling
Console.WriteLine("Hey, how do you feel today?");
feeling = Console.ReadLine(); // takes the input from the user
Console.WriteLine("Hello there! I am feeling "+ feeling + "!"); //
print the output
}
using System;
using System.IO; // defining the System package
class Program2 {
public static void Main() //defining the main method
// declaring variables
int price;
string name;
int icecreams;
int total;
Console.WriteLine("Enter price of each ice-cream: ");
price = Convert.ToInt32(Console.ReadLine()); // Converting string to int
Console.WriteLine("Enter the customer's name: ");
name = Console.ReadLine(); //reading input
Console.WriteLine("Enter the number of icecreams that "+name+" has ordered");
icecreams = Convert.ToInt32(Console.ReadLine()); // Converting string to int
Total = icecreams * price; // calculating total price
// printing the final output
Console.WriteLine(name+" has ordered "+icecreams+
" ice-creams. The total amount to be paid is "+total);
}