C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet473/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   469   470   471   472   473   474   475   476   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

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 (and possibly more). Thus, byte streams are not perfectly 

suited to handling character-based I/O. To solve this problem, the .NET Framework defines 

several classes that convert a byte stream into a character stream, handling the translation of 

byte

-to-


char

 and 


char

-to-


byte

 automatically.



The Predefined Streams

Three predefined streams, which are exposed by 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 

363

CHAPTER


www.freepdf-books.com


364

 

P a r t   I :  



T h e   C #   L a n g u a g e

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.



The Stream Classes

The .NET Framework defines both byte and character stream classes. However, the character 

stream classes are really just wrappers that convert an underlying byte stream to a character 

stream, handling any conversion automatically. Thus, the character streams, while logically 

separate, are built upon byte streams.

The core stream classes are defined within the 



System.IO

 namespace. To use these 

classes, you will usually include the following statement near the top of your program:

using System.IO;

The reason that you don’t have to specify 

System.IO

 for console input and output is that 

the

Console

 class is defined in the 



System

 namespace.



The Stream Class

The core stream class is 



System.IO.Stream

.

Stream

 represents a byte stream and is a base 

class for all other stream classes. It is also abstract, which means that you cannot instantiate 

a

Stream

 object. 



Stream

 defines a set of standard stream operations. Table 14-1 shows 

several commonly used methods defined by 

Stream

.

Several of the methods shown in Table 14-1 will throw an 



IOException

 if an I/O error 

occurs. If an invalid operation is attempted, such as attempting to write to a stream that is 

read-only, a 



NotSupportedException

 is thrown. Other exceptions are possible, depending 

on the specific method.

Method


Description

void Close( )

Closes the stream. 

void Flush( )

Writes the contents of the stream to the physical device.

int ReadByte( )

Returns an integer representation of the next available 

byte of input. Returns –1 when the end of the file is 

encountered.

int Read(byte[ ] 



buf

, int 


offset

,

             int 



numBytes

)

Attempts to read up to 



numBytes

 bytes into 



buf

star ting 

at

buf

[

offset

], returning the number of bytes successfully 

read. 


long Seek(long 

offset

, SeekOrigin 



origin

)

Sets the current position in the stream to the specified 



offset

 from the specified 



origin.

 It returns the new position.

void WriteByte(byte 

b

)

Writes a single byte to an output stream. 



int Write(byte[ ] 

buf

, int 


offset

,

             int 



numBytes

)

Writes a subrange of 



numBytes

 bytes from the array 



buf,

beginning at 



buf

[

offset

]. The number of bytes written is 

returned.

T

ABLE


 14-1 

Some of the Methods Defi ned by 

Stream

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   469   470   471   472   473   474   475   476   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish