Uploaded by cemekachris12

416 Input Output in Console Application

advertisement
INPUT/OUTPUT IN CONSOLE APPLICATION
Console Class in C#
The Console is a window of the operating system through which users can interact with system
programs of the operating system or with other console applications. The interaction consists of text
input from the standard input (usually keyboard) or text display on the standard output (usually on the
computer screen). These actions are also known as input-output operations. The text written on the
console brings some information and is a sequence of characters sent by one or more programs.
For each console application the operating system connects input and output devices. By default these
are the keyboard and the screen but they can be redirected to a file or other devices.
One really useful class that handles input from a user is called the Console class. Console class is present
in the “System” namespace. So, first, we import this System namespace into our program. It also
converts the Bytes (from the input stream) into characters using the platform’s default charset. To use
the Console class, you need to reference it in your code. This is done with the keyword using.
The syntax of a typical program is given below:
using System;
namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
}
}
}
This tells the C# compiler that you want to use the Console class that is located in
the System namespace. This Console class provides a number of built-in methods which we can use the
get the user input and also to print output on the Console window.
CONSOLE OUTPUT
In order to output something in C#, we can use
System.Console.WriteLine()
System.Console.Write()
Here, System is a namespace, Console is a class within namespace System and WriteLine and Write are
methods of class Console.
Example: Let us see a simple example to print a string on the Console window in C#.
using System;
namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
The main difference between WriteLine() and Write() method of Console Class in C# is that the Write()
method only prints the string provided to it, while the WriteLine() method prints the string and moves to
the start of the next line as well.
The WriteLine() and Write() method of the Console class in C# can also be used to print variables and
literals. Let us see an example to see how we can use the WriteLine() and Write() methods to print
Variables and Literals in C#.
using System;
namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
//Printing Variable
int number = 10;
Console.WriteLine(number);
// Printing Literal
Console.WriteLine(50.05);
}
}
}
Strings can also be combined or concatenated using the + operator while printing inside the WriteLine()
and Write() Method in C#. Let us understand this with an example.
using System;
namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
int number = 55;
Console.WriteLine("Hello " + "C#");
Console.WriteLine("Number = " + number);
}
}
}
For printing long and elaborate series of elements, special options (also known as overloads) of the
methods Write(…) and WriteLine(…) have been introduced. These options have a completely different
concept than the standard methods for printing in C#. Their main idea is to adopt a special string,
formatted with special formatting characters and list of values, which should be substituted in place of
“the format specifiers”. This is known as formatted output and is shown below.
string name = "John";
int age = 18;
string town = "Seattle";
Console.Write("{0} is {1} years old from {2}!\n", name, age, town);
The output of the program:
John is 18 years old from Seattle!
From the output of this Write(…) version we saw that the first argument is the format string. Following
is a series of arguments, which are placed where we have a number enclosed in curly brackets. The
expression {0} means to put in its place the first of the arguments submitted after the format string (in
this case name). Next is {1} which means to replace with the second of the arguments (age). The last
placeholder is {2}, which means to replace with the next parameter (town). Last is \n, which is a special
character that indicates moving to a new line.
CONSOLE INPUT
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.
In C#, the simplest method to get input from the user is by using the ReadLine() method of
the Console class. 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.
However, Read() and ReadKey() are also available for getting input from the user. They are also included
in Console class.
Difference between ReadLine(), Read() and ReadKey() method:

ReadLine(): The ReadLine() method reads the next line of input from the standard input stream.
It returns the same string.

Read(): The Read() method reads the next character from the standard input stream. It returns
the ascii value of the character.

ReadKey(): The ReadKey() method obtains the next key pressed by user. This method is usually
used to hold the screen until user press a key.
Example:
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 ExampleRead
{
class orderprocessing
{
static void Main()
{
int Number;
string strNumber;
strNumber = console.ReadLine();
Number = int.parse(strNumber);
}
}
}
Example: Area of a Rectangle or a Triangle
class CalculatingArea
{
static void Main()
{
Console.WriteLine("This program calculates " +
"the area of a rectangle or a triangle");
Console.WriteLine("Enter a and b (for rectangle) " +
"or a and h (for triangle): ");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
Console.WriteLine("Enter 1 for a rectangle or " +
"2 for a triangle: ");
int choice = int.Parse(Console.ReadLine());
double area = (double) (a * b) / choice;
Console.WriteLine("The area of your figure is " + area);
}
}
Output:
This program calculates the area of a rectangle or a triangle
Enter a and b (for rectangle) or a and h (for triangle):
5
4
Enter 1 for a rectangle or 2 for a triangle:
2
The area of your figure is 10
Download