C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet506/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   502   503   504   505   506   507   508   509   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

StringWriter

 called 


strwtr

 and outputs to it using 



WriteLine( )

.

Next, it creates a 



StringReader

 using the string contained in 



strwtr

. This string is obtained 

by calling 

ToString( )

 on 


strwtr

. Finally, the contents of this string are read using 



ReadLine( )

.

www.freepdf-books.com




396

 

P a r t   I :  



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

Converting Numeric Strings to Their Internal Representation

Before leaving the topic of I/O, we will examine a technique useful when reading numeric 

strings. As you know, 

WriteLine( )

 provides a convenient way to output various types of 

data to the console, including numeric values of the built-in types, such as 

int

 and 


double

.

Thus,



WriteLine( )

 automatically converts numeric values into their human-readable form. 

However, a parallel input method that reads and converts strings containing numeric 

values into their internal, binary format is not provided. For example, there is no version of 



Read( )

 that reads from the keyboard a string such as “100” and then automatically converts 

it into its corresponding binary value that can be stored in an 

int

 variable. Instead, there are 

other ways to accomplish this task. Perhaps the easiest is to use a method that is defined for 

all of the built-in numeric types: 



Parse( )

.

Before we begin, it is necessary to state an important fact: All of C#’s built-in types, such 



as

int

 and 


double

, are actually just 



aliases

 (that is, other names) for structures defined by the 

.NET framework. In fact, the C# type and .NET structure type are indistinguishable. One is 

just another name for the other. Because C#’s value types are supported by structures, the 

value types have members defined for them.

For the numeric types, the .NET structure names and their C# keyword equivalents are 

shown here:

.NET Structure Name

C# Name

Decimal


decimal

Double


double

Single


float

Int16


shor t

Int32


int

Int64


long

UInt16


ushor t

UInt32


uint

UInt64


ulong

Byte


byte

SByte


sbyte

These structures are defined inside the 



System

 namespace. Thus, the fully qualified 

name for 

Int32

 is 


System.Int32

. These structures offer a wide array of methods that help 

fully integrate the value types into C#’s object hierarchy. As a side benefit, the numeric 

structures also define a static method called 



Parse( )

 that converts a numeric string into 

its corresponding binary equivalent.

There are several overloaded forms of 



Parse( )

. The simplest version for each numeric 

structure is shown here. It performs the conversion using the default locale and numeric 

style. (Other versions let you perform locale-specific conversions and specify the numeric 

style.) Notice that each method returns a binary value that corresponds to the string.

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   502   503   504   505   506   507   508   509   ...   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