Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is checked keyword in c#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 281
    Comment on it

    Arithmetic operations are very basic in every strongly type programming languages. We will oftenly save the result of this arithmetic computation in certain type of variable.  Suppose if I am adding two integers then i will save the result in integer type variable. Now what happen if the sum is greater than the max value of result variable ? see how C# CLR will respond.

     

    So we can see here result is negative which is very shocking. There is no overflow alert/exception/warning.

    By default, Compiler will raise the compile time error if we have used constants in expressions. See below for more details.

    Here we can compiler raised the overflow exception.

    C# is providing the solution to tackle this issue at run time with checked keyword which force the CLR to throw the overflow exception. Please see below how checked would do the task.

    Here we can see an exception was thrown when result is overflown. We can also make the checked block if we have more than one expression eligible to be overflown. Please see below.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: