Chapter 14: Using IO


The earlier chapters of this book have used parts of the C# I/O system, such as Console.WriteLine( ), but have done so without much formal explanation. Because the C# I/O system is built upon a hierarchy of classes, it was not possible to present its theory and details without first discussing classes, inheritance, and exceptions. Now it is time to examine C#’s approach to I/O in detail. Because C# uses the I/O system and classes defined by the .NET Framework, a discussion of I/O under C# is also a discussion of the .NET I/O system, in general.

This chapter examines C#’s approach to both console I/O and file I/O. Be forewarned that C#’s I/O system is quite large. This chapter describes the most important and commonly used features.

C#’s I/O Is Built Upon Streams

C# programs perform I/O through streams. A stream is an abstraction that either produces or consumes information. A stream is linked to a physical device by the C# I/O system. All streams behave in the same manner, even if the actual physical devices they are linked to differ. Thus, the I/O classes and methods can be applied to many types of devices. For example, the same methods that you use to write to the console can also be used to write to a disk file.

Byte Streams and Character Streams

At the lowest level, all C# I/O operates on bytes. This makes sense because many devices are byte oriented when it comes to I/O operations. Frequently, though, we humans prefer to communicate using characters. Recall that in C#, char is a 16-bit type, and byte is an 8-bit type. If you are using the ASCII character set, then it is easy to convert between char and byte; just ignore the high-order byte of the char value. But this won’t work for the rest of the Unicode characters, which need both bytes. Thus, byte streams are not perfectly suited to handling character-based I/O. To solve this problem, C# defines several classes that convert a byte stream into a character stream, handling the translation of byte-to-char and char-to-byte for you automatically.

The Predefined Streams

Three predefined streams, which are exposed through the properties called Console.In, Console.Out, and Console.Error, are available to all programs that use the System namespace. Console.Out refers to the standard output stream. By default, this is the console. When you call Console.WriteLine( ), for example, it automatically sends information to Console.Out. Console.In refers to standard input, which is by default the keyboard. Console.Error refers to the standard error stream, which is also the console by default. However, these streams can be redirected to any compatible I/O device. The standard streams are character streams. Thus, these streams read and write characters.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net