C# 0 The Complete Reference


if  succeeds because  b



Download 4,07 Mb.
Pdf ko'rish
bet576/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   572   573   574   575   576   577   578   579   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

if

 succeeds because 



b

 is an object of type 



B

, which is derived from type 



A

. Thus, 


b

is

 an 


A

. However, the reverse is not true. When this line is executed,

if(a is B)

  Console.WriteLine("This won’t display -- a not derived from B");

the

if

 does not succeed, because 



a

 is of type 



A

, which is not derived from 



B

. Thus, 


a

is not

B

.

Using as

Sometimes you will want to try a conversion at runtime, but not throw an exception if the 

conversion fails (which is the case when a cast is used). To do this, use the 



as

 operator, 

which has this general form:

expr

 as 


type

www.freepdf-books.com




PART I

C h a p t e r   1 7 :  

R u n t i m e   T y p e   I D ,   R e f l e c t i o n ,   a n d   A t t r i b u t e s  

461


PART IPART I

Here, 


expr

 is the expression being converted to 



type.

 If the conversion succeeds, then a 

reference to 

type

 is returned. Otherwise, a null reference is returned. The 



as

 operator can be 

used to perform only reference, boxing, unboxing, or identity conversions.

The


as

 operator offers a streamlined alternative to 



is

 in some cases. For example, 

consider the following program that uses 

is

 to prevent an invalid cast from occurring:

// Use is to avoid an invalid cast.

using System;

class A {}

class B : A {}

class CheckCast {

  static void Main() {

    A a = new A();

    B b = new B();

    // Check to see if a can be cast to B.

    if(a is B)  // if so, do the cast

      b = (B) a;

    else // if not, skip the cast

      b = null;

    if(b==null)

      Console.WriteLine("The cast in b = (B) a is NOT allowed.");

    else


      Console.WriteLine("The cast in b = (B) a is allowed");

  }


}

This program displays the following output:

The cast in b = (B) a is NOT allowed.

As the output shows, since 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   572   573   574   575   576   577   578   579   ...   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