C# 0 The Complete Reference



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

Int32.Parse( )

 on a string that contains 

a floating-point value will not produce the desired result.

As explained, 



Parse( )

 will throw an exception on failure. You can avoid generating an 

exception when converting numeric strings by using the 

TryParse( )

 method, which is defined 

for all of the numeric structures. Here is an example. It shows one version of 

TryParse( )

 as 


defined by 

Int32

.

static bool TryParse(string 



str

, out int 



result

)

The numeric string is passed in 



str

. The result is returned in 



result

. It performs the 

conversion using the default locale and numeric style. (A second version of 

TryParse( )

is available that lets you specify the numeric style and locale.) If the conversion fails, such 

as when 

str

 does not contain a numeric string in the proper form, 



TryParse( )

 returns false. 

Otherwise, it returns true. Therefore, you must check the return value to confirm that a 

successful conversion has occurred.

www.freepdf-books.com



15

Delegates, Events, and 

Lambda Expressions

T

his chapter examines three innovative C# features: delegates, events, and lambda 



expressions. A 

delegate

provides a way to encapsulate a method. An 



event

is a 


notification that some action has occurred. Delegates and events are related because 

an event is built upon a delegate. Both expand the set of programming tasks to which C# 

can be applied. The 

lambda expression

 is a new syntactic feature provided by C# 3.0. It offers 

a streamlined, yet powerful way to define what is, essentially, a unit of executable code. 

Lambda expressions are often used when working with delegates and events because a 

delegate can refer to a lambda expression. (Lambda expressions are also very important to 

LINQ, which is described in Chapter 19.) Also examined are anonymous methods, covariance, 

contravariance, and method group conversions.

Delegates

Let’s begin by defining the term 



delegate.

 In straightforward language, a delegate is an object 

that can refer to a method. Therefore, when you create a delegate, you are creating an object 

that can hold a reference to a method. Furthermore, the method can be called through this 

reference. In other words, a delegate can invoke the method to which it refers. As you will 

see, this is a very powerful concept.

It is important to understand that the same delegate can be used to call different 

methods during the runtime of a program by simply changing the method to which the 

delegate refers. Thus, the method that will be invoked by a delegate is not determined at 

compile time, but rather at runtime. This is the principal advantage of a delegate.




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   505   506   507   508   509   510   511   512   ...   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