C# 0 The Complete Reference


ByTwos  and  ByTwos



Download 4,07 Mb.
Pdf ko'rish
bet666/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   662   663   664   665   666   667   668   669   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

ByTwos

 and 


ByTwos

 would be invalid.



Comparing Instances of a Type Parameter

Sometimes you will want to compare two instances of a type parameter. For example, you 

might want to write a generic method called 

IsIn( )

 that returns true if some value is 

contained within an array. To accomplish this, you might first try something like this:

// This won’t work!

public static bool IsIn(T what, T[] obs) {

  foreach(T v in obs)

    if(v == what) // Error!

      return true;

  return false;

}

Unfortunately, this attempt won’t work. Because 



T

 is a generic type, the compiler has no 

way to know precisely how two objects should be compared for equality. Should a bitwise 

comparison be done? Should only certain fields be compared? Should reference equality be 

used? The compiler has no way to answer these questions.

To enable two objects of a generic type parameter to be compared, you must use the 



CompareTo( )

 method defined by one of .NET’s standard interfaces: 



IComparable

. This 


interface is implemented by all of C#’s built-in types, including 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   662   663   664   665   666   667   668   669   ...   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