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.
bet1/8
Sana11.01.2022
Hajmi33,56 Kb.
#347199
  1   2   3   4   5   6   7   8
Bog'liq
si, as operatori OLMOSBEK


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 indicating whether or not the instance in question is in fact of the type specified as the second operand.

Here is an example:

C# Is Operator Example

1 using System;

2

3 class Example

4 {

5 static void Main()

6 {

7 Console.WriteLine("hello, world" is string); // True

8 }

9 }

Obviously, this is a simple example but I am sure that the reader is capable of extrapolating more practical usages of is.



Problem With C# Is Operator

There is an inherent problem with is operator that many developers are not aware of:

Under the covers, the is operator uses a cast to make its determination.

Why is this a problem?

Because the next step most developers take is to cast the instance to the type. The is operator viewed as a sort of “safe cast” since you know that the instance is of that type. Here is an example of this practice:

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 if (obj is Dog)

15 ((Dog)obj).Speak(); // Bark!

16 }

17 }

In this example there are actually two separate casts, the is and the explicit cast below it (inside the conditional statement). You shouldn’t cast more then once if you don’t need to do so. Also, the is check does not consider user-defined conversions (such as implicit and explicit cast operators) so is may return false for an instance that could have successfully been casted.

So what should we do instead?

One method would be to use is operator with a variable name. Like this,



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 if (obj is Dog dog)

15 dog.Speak(); // Bark!

16 }

17 }

Or use C#’s true safe casting operator: as.



C# As Keyword

The as operator allows you to do an explicit cast on a type while avoiding any InvalidCastExceptions that may occur by assigning null to the variable if the cast fails.

Here is an example of how to use as:


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