C# 0 The Complete Reference



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

is

,

as

, and 

typeof

.

Each is examined in turn.



Testing a Type with is

You can determine if an object is of a certain type by using the 



is

 operator. Its general form 

is shown here:

expr

 is 


type

Here, 


expr

 is an expression that describes an object whose type is being tested against 



type.

 If 


the type of 

expr

 is the same as, or compatible with



type,

 then the outcome of this operation 

is true. Otherwise, it is false. Thus, if the outcome is true

expr

 is some form of 



type.

 As it 


applies to 

is

, one type is compatible with another if both are the same type, or if a reference, 

boxing, or unboxing conversion exists.

459


CHAPTER

www.freepdf-books.com




460

 

P a r t   I :  



T h e   C #   L a n g u a g e

Here is an example that uses 



is

:

// Demonstrate is.



using System;

class A {}

class B : A {}

class UseIs {

  static void Main() {

    A a = new A();

    B b = new B();

    if(a is A) Console.WriteLine("a is an A");

    if(b is A)

      Console.WriteLine("b is an A because it is derived from A");

    if(a is B)

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

    if(b is B) Console.WriteLine("B is a B");

    if(a is object) Console.WriteLine("a is an object");

  }

}

The output is shown here:



a is an A

b is an A because it is derived from A

B is a B

a is an object

Most of the 

is

 expressions are self-explanatory, but two may need a little discussion. 

First, notice this statement:

if(b is A)

  Console.WriteLine("b is an A because it is derived from A");

The



Download 4,07 Mb.

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