C# Is Operator C# has an interesting operator know as is. The is operator is used for execution-time type checking. It is a binary operator that returns a Boolean



Download 33,56 Kb.
bet2/8
Sana11.01.2022
Hajmi33,56 Kb.
#347199
1   2   3   4   5   6   7   8
Bog'liq
si, as operatori OLMOSBEK

As In C# Example

1 using System;

2

3 class Dog

4 {

5 public void Speak() { Console.WriteLine("Bark!"); }

6 }

7

8 class Example

9 {

10 static void Main()

11 {

12 Object obj = new Dog();

13

14 Dog dog = obj as Dog;

15 if (dog != null)

16 dog.Speak();

17 }

18 }

This example only contains one cast and also allows you to avoid any exceptions that may occur at execution time.

So, the important thing to remember is that if you feel like you need to use is, most likely you ought to use as.

However,


Be careful that you are not using casting to solve a problem that would be better solved with polymorphism. If you find that you are doing multiple checks for different types, casting, and then calling methods on those types, you should reevaluate your approach to see if a polymorphic solution would be better. Casting is a powerful tool but it can be misapplied.


Download 33,56 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8




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