C# 0 The Complete Reference


Use ref and out Parameters



Download 4,07 Mb.
Pdf ko'rish
bet245/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   241   242   243   244   245   246   247   248   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Use ref and out Parameters

As just explained, value types, such as 



int

 or 


char

, are passed by value to a method. This 

means that changes to the parameter that receives a value type will not affect the actual 

argument used in the call. You can, however, alter this behavior. Through the use of the 



ref

and


out

 keywords, it is possible to pass any of the value types by reference. Doing so allows 

a method to alter the argument used in the call.

Before going into the mechanics of using 



ref

 and 


out

, it is useful to understand why you 

might want to pass a value type by reference. In general, there are two reasons: to allow a 

method to alter the contents of its arguments or to allow a method to return more than one 

value. Let’s look at each reason in detail.

Often you will want a method to be able to operate on the actual arguments that are 

passed to it. The quintessential example of this is a 

Swap( )

 method that exchanges the values 

of its two arguments. Since value types are passed by value, it is not possible to write a 

method that swaps the value of two 



int

s, for example, using C#’s default call-by-value 

parameter passing mechanism. The 

ref

 modifier solves this problem.

As you know, a 

return

 statement enables a method to return a value to its caller. However, a 

method can return 

only one

 value each time it is called. What if you need to return two or more 

pieces of information? For example, what if you want to create a method that decomposes a 

floating-point number into its integer and fractional parts? To do this requires that two pieces 

of information be returned: the integer portion and the fractional component. This method 

cannot be written using only a single return value. The 



out

 modifier solves this problem.

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   241   242   243   244   245   246   247   248   ...   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