C# 0 The Complete Reference


Using checked and unchecked



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

Using checked and unchecked

A special feature in C# relates to the generation of overflow exceptions in arithmetic 

computations. As you know, it is possible for some types of arithmetic computations to 

produce a result that exceeds the range of the data type involved in the computation. When 

this occurs, the result is said to 

overflow.

 For example, consider the following sequence:

byte a, b, result;

a = 127;


b = 127;

result = (byte)(a * b);

Here, the product of 

a

 and 


b

 exceeds the range of a 



byte

 value. Thus, the result overflows 

the type of the result.

C# allows you to specify whether your code will raise an exception when overflow 

occurs by using the keywords 

checked

 and 


unchecked

. To specify that an expression be 

checked for overflow, use 

checked

. To specify that overflow be ignored, use 



unchecked

.

In this case, the result is truncated to fit into the target type of the expression.



The

checked

 keyword has these two general forms. One checks a specific expression 

and is called the 

operator form

 of 


checked

. The other checks a block of statements and is 

called the 

statement form.

checked (



expr

)

checked {



    // statements to be checked

}

Here, 



expr

 is the expression being checked. If a checked expression overflows, then an 



OverflowException

 is thrown.

The

unchecked

 keyword also has two general forms. The first is the operator form, 

which ignores overflow for a specific expression. The second ignores overflow for a block 

of statements.

unchecked (

expr

)

unchecked {



    // statements for which overfl ow is ignored

}

Here, 



expr

 is the expression that is not being checked for overflow. If an unchecked 

expression overflows, then truncation will occur.

Here is a program that demonstrates both 



checked

 and 


unchecked

:

// Using checked and unchecked.



using System;

class CheckedDemo {

  static void Main() {

    byte a, b;

www.freepdf-books.com



Download 4,07 Mb.

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