Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# checked block

Tags:

Can someone explain to me what exactly is the checked an unchecked block ?
And when should I use each ?

like image 289
Yochai Timmer Avatar asked Mar 07 '11 09:03

Yochai Timmer


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

Arithmetic overflow; for example:

int i = int.MaxValue -10; checked {           i+= 20; // boom: OverflowException            // "Arithmetic operation resulted in an overflow." } 

So use checked when you don't want accidental overflow / wrap-around to be a problem, and would rather see an exception.

unchecked explicitly sets the mode to allow overflow; the default is unchecked unless you tell the compiler otherwise - either through code (above) or a compiler switch (/checked in csc).

like image 130
Marc Gravell Avatar answered Sep 21 '22 07:09

Marc Gravell