C# 0 The Complete Reference


PART I C h a p t e r   4



Download 4,07 Mb.
Pdf ko'rish
bet131/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   127   128   129   130   131   132   133   134   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   4 :  

O p e r a t o r s  

71

PART IPART I



division. Thus, in the first test

d

 is 2 and the modulus operation is performed. The second 

test fails because 

d

 is set to zero, and the modulus operation is skipped, avoiding a divide-

by-zero error. Finally, the normal AND operator is tried. This causes both operands to be 

evaluated, which leads to a runtime error when the division-by-zero occurs.

Since the short-circuit operators are, in some cases, more efficient than their normal 

counterparts, you might be wondering why C# still offers the normal AND and OR 

operators. The answer is that in some cases you will want both operands of an AND or OR 

operation to be evaluated because of the side effects produced. Consider the following:

// Side effects can be important.

using System;

class SideEffects {

  static void Main() {

    int i;

    bool someCondition = false;

    i = 0;

    // Here, i is still incremented even though the if statement fails.

    if(someCondition & (++i < 100))

       Console.WriteLine("this won't be displayed");

    Console.WriteLine("if statement executed: " + i); // displays 1

    // In this case, i is not incremented because the short-circuit

    // operator skips the increment.

    if(someCondition && (++i < 100))

      Console.WriteLine("this won't be displayed");

    Console.WriteLine("if statement executed: " + i); // still 1 !!

  }

}

First, notice that the 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   127   128   129   130   131   132   133   134   ...   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