Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
BRH 58
#ASP.NET 55
ASP.NET 50
#DOTNET 49
.NET 40
WCF 21
DOTNET 12
c# 8
windows azure 7
SILVERLIGHT 7

Archive · View All
April 2011 9
March 2011 9
February 2011 8
December 2010 7
November 2010 5
September 2010 5
August 2010 5
May 2011 4
October 2010 4
January 2011 2

Basics of C#: Checked and Unchecked Conversions

Aug 18 2010 6:14AM by Dhananjay Kumar   

When we do conversion in c# , sometime conversions are not checked. C# compiler may not check the conversion and truncate the result. So Checked keyword helps us in doing that.

Objective

In this article, I will discuss about Checked and unchecked keyword and conversions in C#.

Consider the below code

Program.cs

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;

namespace ConsoleApplication7
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            int number = int.MaxValue;
            number = number + 1;
            Console.WriteLine(number);
            Console.Read();
        }
    }
}

Now, if you see the above code

  1. We are taking maximum integer value in a variable. And that is 2147483647.
    int number = int.MaxValue;
  2. We are increasing the number by 1. Now here it should get overflow because an integer variable cannot hold a number greater than 2147483647.
    number = number + 1;

So, now data will overflow and truncate during the assignment.

So, as the output we will get

clip_image004

If you notice above output, number assigned is truncated to -2147483647

It happened because compiler is doing unchecked compilation

So forcefully do the checked compilation, we will use CHECKEDkeyword. Just put all the codes in between checked block

static void Main(string[] args)
{
    checked
    {
        // Your Code for checked compilation
    }
}

So, now to avoid truncating during overflow assignment, we need to put our code in between Checked block.

So, modify the above code as below

Programs.cs

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;

namespace ConsoleApplication7
{
    classProgram
    {
        staticvoid Main(string[] args)
        {

            checked
            {
                int number = int.MaxValue;
                number = number + 1;
                Console.WriteLine(number);
                Console.Read();
            }

        }
    }
}

Now if you run the above code, you will get the below error.

clip_image007

It shows if the code is in checked block then rather than truncating at overflow, compiler is throwing an exception.

If we are doing checked compilation then to override that, we can put our code in Unchecked Block

unchecked
    {
        // Your Code to override
        // Checked compilation
    }

You can change the behavior of compiler from checked to unchecked through command prompt.

Tags: #DOTNET, DOTNET, #ASP.NET, ASP.NET, c#, BRH, .NET,


Dhananjay Kumar
49 · 4% · 1198
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

1  Comments  

  • This is so cool! Never knew this existed! I believe this should be made standard practice for at least the key modules of any system.

    commented on Aug 28 2010 7:43AM
    Nakul Vachhrajani
    4 · 33% · 10587

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]