Chapter108:CheckedandUnchecked

No Comments

Section108.1:CheckedandUnchecked

C#statementsexecutesineithercheckedoruncheckedcontext.Inacheckedcontext,arithmeticoverflowraisesan exception. In an unchecked context, arithmetic overflow is ignored and the result is truncated.

shortm=32767;

shortn=32767;

intresult1=              checked((short)(m+n));                //willthrowanOverflowException

intresult2=              unchecked((short)(m+n));//willreturn-2

Ifneitherofthesearespecifiedthenthedefaultcontextwillrelyonotherfactors,suchascompileroptions.

Section108.2: Checkedand Uncheckedas a scope

Thekeywordscanalsocreatescopesinorderto(un)checkmultipleoperations.

shortm =32767;

shortn=32767;

checked

{

intresult1=(short)(m+n);//willthrowanOverflowException

}

unchecked

{

intresult2=(short)(m+n);//willreturn-2

}

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts
No Comments

Leave a Comment