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



Download 21,46 Mb.
Pdf ko'rish
bet432/1830
Sana28.09.2021
Hajmi21,46 Mb.
#188411
1   ...   428   429   430   431   432   433   434   435   ...   1830
Bog'liq
[book] [en] Pro C# 2008 and the NET 3.5 Platform Fourth Edition

// Catch a possible invalid cast.

try


{

Hexagon hex = (Hexagon)frank;

}

catch (InvalidCastException ex)



{

Console.WriteLine(ex.Message);

}

While this is a fine example of defensive programming, C# provides the as keyword to quickly



determine at runtime whether a given type is compatible with another. When you use the as key-

word, you are able to determine compatibility by checking against a null return value. Consider the

following:

// Use 'as' to test compatability.

Hexagon hex2 = frank as Hexagon;

if (hex2 == null)

Console.WriteLine("Sorry, frank is not a Hexagon...");



The C# is Keyword

Given that the FireThisPerson() method has been designed to take any possible type derived from

Employee, one question on your mind may be how this method can determine which derived type

was sent into the method. On a related note, given that the incoming parameter is of type Employee,

how can you gain access to the specialized members of the SalesPerson and Manager types?

In addition to the as keyword, the C# language provides the is keyword to determine whether

two items are compatible. Unlike the as keyword, however, the is keyword returns false, rather

C H A P T E R   6  



U N D E R S TA N D I N G   I N H E R I TA N C E  A N D   P O LY M O R P H I S M



211

8849CH06.qxd  10/1/07  10:35 AM  Page 211




than a null reference, if the types are incompatible. Consider the following implementation of the

FireThisPerson() method:

static void FireThisPerson(Employee emp)

{

if (emp is SalesPerson)



{

Console.WriteLine("Lost a sales person named {0}", emp.Name);

Console.WriteLine("{0} made {1} sale(s)...", emp.Name,

((SalesPerson)emp).SalesNumber);

Console.WriteLine();

}

if (emp is Manager)



{

Console.WriteLine("Lost a suit named {0}", emp.Name);

Console.WriteLine("{0} had {1} stock options...", emp.Name,

((Manager)emp).StockOptions);

Console.WriteLine();

}

}



Here you are performing a runtime check to determine what the incoming base class reference

is actually pointing to in memory. Once you determine whether you received a SalesPerson or

Manager type, you are able to perform an explicit cast to gain access to the specialized members of

the class. Also notice that you are not required to wrap your casting operations within a try/catch

construct, as you know that the cast is safe if you enter either if scope, given our conditional check.


Download 21,46 Mb.

Do'stlaringiz bilan baham:
1   ...   428   429   430   431   432   433   434   435   ...   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