C# 0 The Complete Reference


PART I C h a p t e r   2 0



Download 4,07 Mb.
Pdf ko'rish
bet762/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   758   759   760   761   762   763   764   765   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   2 0 :  

U n s a f e   C o d e ,   P o i n t e r s ,   N u l l a b l e   T y p e s ,   a n d   M i s c e l l a n e o u s   T o p i c s  

601


PART IPART I

    if(result.HasValue)

      Console.WriteLine("result has this value: " + result.Value);

    else


      Console.WriteLine("result has no value");

  }


}

The output is shown here:

result has no value

result has this value: 110



The ?? Operator

If you attempt to use a cast to convert a nullable object to its underlying type, a 



System.InvalidOperationException

 will be thrown if the nullable object contains a 



null

value. This can occur, for example, when you use a cast to assign the value of a nullable 

object to a variable of its underlying type. You can avoid the possibility of this exception 

being thrown by using the 



??

 operator, which is called the 



null coalescing operator

. It lets you 

specify a default value that will be used when the nullable object contains 

null

. It also 

eliminates the need for the cast.

The


??

 operator has this general form:



nullable-object

 ?? 


default-value

If

nullable-object

contains a value, then the value of the 

??

 is that value. Otherwise, the value 

of the 

??

 operation is 



default-value

.

For example, in the following code 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   758   759   760   761   762   763   764   765   ...   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