This print for content only-size & color not accurate 7" X 9-1/4" / casebound / malloy



Download 21,46 Mb.
Pdf ko'rish
bet654/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   650   651   652   653   654   655   656   657   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Must supply type parameter if

// the method does not take params.

DisplayBaseClass();

DisplayBaseClass();

// Compiler error! No params? Must supply placeholder!

// DisplayBaseClass();

Console.ReadLine();

}

Figure 10-2 shows the current output of this application.



C H A P T E R   1 0  

C O L L E C T I O N S   A N D   G E N E R I C S



328

8849CH10.qxd  9/25/07  4:17 PM  Page 328




Figure 10-2.

Generic methods in action

Currently, the generic Swap and DisplayBaseClass methods have been defined within

the application object (i.e., the type defining the Main() method). Of course, like any method, if

you would rather define these members in a separate class type (MyGenericMethods), you are free

to do so:

public static class MyGenericMethods

{

public static void Swap(ref T a, ref T b)



{

Console.WriteLine("You sent the Swap() method a {0}",

typeof(T));

T temp;


temp = a;

a = b;


b = temp;

}

public static void DisplayBaseClass()



{

Console.WriteLine("Base class of {0} is: {1}.",

typeof(T), typeof(T).BaseType);

}

}



Because the static Swap and DisplayBaseClass methods have been scoped within a

new static class type, you will need to specify the type’s name when invoking either member, for

example:

MyGenericMethods.Swap(ref a, ref b);

Of course, generic methods do not need to be static. If Swap and DisplayBaseClass

were instance level (and defined in a nonstatic class), you would simply make an instance of

MyGenericMethods and invoke them off the object variable:

MyGenericMethods c = new MyGenericMethods();

c.Swap(ref a, ref b); 

C H A P T E R   1 0  



C O L L E C T I O N S   A N D   G E N E R I C S



329

8849CH10.qxd  9/25/07  4:17 PM  Page 329





Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   650   651   652   653   654   655   656   657   ...   1830




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