C# 0 The Complete Reference



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

CopyInsert( )

 is called within 



Main( )

 by use of the normal call syntax, 

without the need to specify type arguments. This is because the types of the arguments are 

automatically discerned, and the type of 



T

 is adjusted accordingly. This process is called 



type inference.

 For example, in the first call:

ArrayUtils.CopyInsert(99, 2, nums, nums2);

the type of 



T

 becomes 



int

 because 99 and the element types of 



nums

 and 


nums2

 are 


int

. In 


the second call, 

string

 types are used, and 



T

 is replaced by 



string

.

Now, notice the commented-out code, shown here:



//    ArrayUtils.CopyInsert(0.01, 2, nums, nums2);

If you remove the comments and then try to compile the program, you will receive an error. 

The reason is that the type of the first argument is 

double

, but the element types of 



nums

and


nums2

 are 


int

. However, all three types must be substituted for the same type 

parameter, 

T.

 This causes a type-mismatch, which results in a compile-time error. This 

ability to enforce type safety is one of the most important advantages of generic methods.

The syntax used to create 



CopyInsert( )

 can be generalized. Here is the general form of a 

generic method:

ret-type meth-name

(

param-list

) { // ...

In all cases, 



type-param-list

is a comma-separated list of type parameters. Notice that for a 

generic method, the type parameter list follows the method name.

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   652   653   654   655   656   657   658   659   ...   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