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


// Note that we now have a default constructor constraint



Download 21,46 Mb.
Pdf ko'rish
bet673/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   669   670   671   672   673   674   675   676   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Note that we now have a default constructor constraint.

public class MyList where T : new()

{

private List listOfData = new List();



public virtual void PrintList(T data) { }

}

// Derived type must honor constraints.

public class MyReadOnlyList : MyList where T : new()

{

public override void PrintList(T data) { }



}

Again, in your day-to-day programming tasks, creating custom generic class hierarchies will

most likely not be a very common task. Nevertheless, doing so is completely possible (as long as you

abide by the rules).



Creating Generic Interfaces

As you saw earlier in the chapter during the examination of the System.Collections.Generic name-

space, generic interfaces are also permissible (e.g., IEnumerable). You are, of course, free to

define your own generic interfaces (with or without constraints). Assume you wish to define an

interface that can perform binary operations on a generic type parameter:

public interface IBinaryOperations where T : struct

{

T Add(T arg1, T arg2);



T Subtract(T arg1, T arg2);

T Multiply(T arg1, T arg2);

T Divide(T arg1, T arg2);

}

Of course, interfaces are more or less useless until they are implemented by a class or structure.



When you implement a generic interface, the supporting type specifies the placeholder type:

public class BasicMath : IBinaryOperations

{

public int Add(int arg1, int arg2)



{ return arg1 + arg2; }

public int Subtract(int arg1, int arg2)

{ return arg1 - arg2; }

public int Multiply(int arg1, int arg2)

{ return arg1 * arg2; }

public int Divide(int arg1, int arg2)

{ return arg1 / arg2; }

}

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



338

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




At this point, you make use of BasicMath as you would expect:

static void Main(string[] args)

{

Console.WriteLine("***** Generic Interfaces *****\n");



BasicMath m = new BasicMath();

Console.WriteLine("1 + 1 = {0}", m.Add(1, 1));

Console.ReadLine();

}

If you would rather create a BasicMath class that operates on floating-point numbers, you could



specify the type parameter as follows:

public class BasicMath : IBinaryOperations<float>

{

public float Add(float arg1, float arg2)



{ return arg1 + arg2; }

...


}

In this case, the compiler will ensure that we pass in a float to each method of the BasicMath

class. You may recall from Chapter 3 that floating-point literal values default to a double, therefore

we must add the suffix F to inform the compiler we do indeed require a float:

static void Main(string[] args)

{

Console.WriteLine("***** Generic Interfaces *****\n");



BasicMath m = new BasicMath();

Console.WriteLine("1.98 + 1.3 = {0}", m.Add(1.98F, 1.3F));

Console.ReadLine();

}


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   669   670   671   672   673   674   675   676   ...   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