Basic Input/Output operations in C#
Posted by letmetutoryou on December 22, 2008
This blog I will show how to perform command-based input/output operations in C# by using the Console class. I will show you how to display information by using the Write and WriteLine methods, and how to gather input information from the keyboard by using the Read and ReadLine methods.
The Console Class
The Console class provides a C# application with access to the standard input, standard output, and standard error streams. Standard input is normally associated with the keyboard—anything that the user types on the keyboard can be read from the standard input stream. Similarly, the standard output stream is usually directed to the screen, as is the standard error stream.
These streams and the Console class are only meaningful to console applications. These are applications that run in a Command window.
Write and WriteLine Methods
You can use the Console.Write and Console.WriteLine methods to display information on the console screen. These two methods are very similar; the
main difference is that WriteLine appends a new line/carriage return pair to the end of the output, and Write does not. Both methods are overloaded. You can call them with variable numbers and types of parameters. For example, you can use the following code to write “99” to the screen:
Console.WriteLine(99);
You can use the following code to write the message “Hello, World” to the screen:
Console.WriteLine(“Hello, World”);
Text Formatting
You can use more powerful forms of Write and WriteLine that take a format string and additional parameters. The format string specifies how the data is
output, and it can contain markers, which are replaced in order by the parameters that follow. For example, you can use the following code to display the message “The sum of 100 and 130 is 230”:
Console.WriteLine(“The sum of {0} and {1} is {2}”, 100, 130, 100+130);
You can use the format string parameter to specify field widths and whether values should be left or right justified in these fields, as shown in the following code:
Console.WriteLine(“\”Left justified in a field of width 10:{0, -10}\””, 99);
Console.WriteLine(“\”Right justified in a field of width 10:{0,10}\””, 99);
This will display the following on the console:
“Left justified in a field of width 10: 99 “
“Right justified in a field of width 10: 99”
You can use the backward slash (\) character in a format string to turn off the special meaning of the character that follows it. For example, “\{” will cause a literal “{” to be displayed, and “\\” will display a literal “\”. You can use the at sign (@) character to represent an entire string verbatim. For example, @”\\server\share” will be processed as \\server\share.
Numeric Formatting
You can also use the format string to specify how numeric data is to be formatted. The full syntax for the format string is {N,M:FormatString}, where N is the parameter number, M is the field width and justification, and FormatString specifies how numeric data should be displayed. The table below summarizes the items that may appear in FormatString. In all of these formats, the number of digits to be displayed, or rounded to, can optionally be specified.
Item | Meaning |
C | Display the number as currency, using the local currency symbol and conventions. |
D | Display the number as a decimal integer. |
E | Display the number by using exponential (scientific) notation. |
F | Display the number as a fixed-point value. |
G | Display the number as either fixed point or integer, depending on which format is the most compact. |
N | Display the number with embedded commas. |
X | Display the number by using hexadecimal notation. |
The following code shows some examples of how to use numeric formatting:
Console.WriteLine(“Currency formatting – {0:C} {1:C4}”, 88.8,-888.8);
Console.WriteLine(“Integer formatting – {0:D5}”, 88);
Console.WriteLine(“Exponential formatting – {0:E}”, 888.8);
Console.WriteLine(“Fixed-point formatting – {0:F3}”,888.8888);
Console.WriteLine(“General formatting – {0:G}”, 888.8888);
Console.WriteLine(“Number formatting – {0:N}”, 8888888.8);
Console.WriteLine(“Hexadecimal formatting – {0:X4}”, 88);
When the previous code is run, it displays the following:
Currency formatting – $88.80 ($888.8000)
Integer formatting – 00088
Exponential formatting – 8.888000E+002
Fixed-point formatting – 888.889
General formatting – 888.8888
Number formatting – 8,888,888.80
Hexadecimal formatting – 0058
Read and ReadLine Methods
You can obtain user input from the keyboard by using the Console.Read and Console.ReadLine methods.
The Read Method
Read reads the next character from the keyboard. It returns the int value –1 if there is no more input available. Otherwise it returns an int representing the character read.
The ReadLine Method
ReadLine reads all characters up to the end of the input line (the carriage return character). The input is returned as a string of characters. You can use the following code to read a line of text from the keyboard and display it to the screen:
string input = Console.ReadLine( );
Console.WriteLine(“{0}”, input);
Pradeep said
Very nice article many will not be dealing with numeric formatting and thanks a lot for providing nice info