C# 0 The Complete Reference


checked  and  unchecked



Download 4,07 Mb.
Pdf ko'rish
bet470/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   466   467   468   469   470   471   472   473   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

checked

 and 


unchecked

 for a single 

expression. The following program shows how to check and uncheck a block of statements.

// Using checked and unchecked with statement blocks.

using System;

class CheckedBlocks {

  static void Main() {

    byte a, b;

    byte result;

    a = 127;

    b = 127;

    try {

      unchecked {

        a = 127;

        b = 127;

        result = unchecked((byte)(a * b));

        Console.WriteLine("Unchecked result: " + result);

        a = 125;

        b = 5;

        result = unchecked((byte)(a * b));

        Console.WriteLine("Unchecked result: " + result);

      }


www.freepdf-books.com


362

 

P a r t   I :  



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

      checked {

        a = 2;

        b = 7;

        result = checked((byte)(a * b)); // this is OK

        Console.WriteLine("Checked result: " + result);

        a = 127;

        b = 127;

        result = checked((byte)(a * b)); // this causes exception

        Console.WriteLine("Checked result: " + result); // won't execute

      }

    }


    catch (OverflowException exc) {

      Console.WriteLine(exc);

    }

  }


}

The output from the program is shown here:

Unchecked result: 1

Unchecked result: 113

Checked result: 14

System.OverflowException: Arithmetic operation resulted in an overflow.

   at CheckedBlocks.Main()

As you can see, the unchecked block results in the overflow being truncated. When 

overflow occurred in the checked block, an exception was raised.

One reason that you may need to use 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   466   467   468   469   470   471   472   473   ...   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