C# 0 The Complete Reference


Creating a Default Value of a Type Parameter



Download 4,07 Mb.
Pdf ko'rish
bet653/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   649   650   651   652   653   654   655   656   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Creating a Default Value of a Type Parameter

When writing generic code, there will be times when the difference between value types and 

reference types is an issue. One such situation occurs when you want to give a variable of a 

type parameter a default value. For reference types, the default value is 



null

. For non-



struct

value types, the default value is 0. The default value for a 



struct

 is an object of that 



struct

with all fields set to their defaults. Thus, trouble occurs if you want to give a variable of a 

type parameter a default value. What value would you use: 

null

, 0, or something else?

For example, given a generic class called 

Test

 declared like this:

class Test {

  T obj;


  // ...

if you want to give 



obj

 a default value, would you use

obj = null; // works only for reference types

or

obj = 0; // works only for numeric types and enums, but not structs



The solution to this problem is to use another form of 

default

, shown here:

default(

type

)

This is the operator form of 



default

, and it produces a default value of the specified 



type,

no

matter what type is used. Thus, continuing with the example, to assign 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   649   650   651   652   653   654   655   656   ...   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