Checked and unchecked keyword in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 4869 red flag
Rating: 5 out of 5  
 1 vote(s)

How to use checked and unchecked keyword in C#

Checked and unchecked keyword in C#

In this article we will learn what is checked and unchecked keyword and will learn how to use them.  Before starting with Check and uncheck keyword we have to know the range of any data type. For example if we use Int32 as data type then what the maximum number we can able to store in this data type. Let’s see in example.

Checked keyword

In this example we will see the maximum and minimum range of Int32

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace DesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Minimum Value :- " + Int32.MinValue);
            Console.WriteLine("Maximum Value:- "  + Int32.MaxValue);
            Console.ReadLine();
        }
    }
}

In Int32 class MinValue and MaxValue properties are available to detect maximum and minimum value of Integer variable. Here is the output


In output, we are seeing the maximum and minimum value of Int32.  Now in next example we will add two integers where the result will be more than actual maximum capacity of integer variable. Have a look on below code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace DesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
           Int32 a = 1234567890;
           Int32 b = 1234567890;
           Int32 c = a + b;
           Console.ReadLine();
        }
    }
}

Here is output screen.


But it’s showing negative value where we are adding two positive values. Actually the resultant value is out of maximum range so it’s showing some garbage value (Actually there is logic behind it and we can evaluate why this negative value is coming) . Now ,in application if we perform same type of operation it will show wrong result and program will show unexpected result. We can use check keyword to prevent this problem. Have a look on below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace DesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
           Int32 a = 1234567890;
           Int32 b = 1234567890;
           Int32 c = checked(a + b);
          
           Console.WriteLine("Value of C is= " + c);
          
 
           Console.ReadLine();
        }
    }
}
 

Here before to initialize variable c we are checking the resultant value using checked keyword. When we will try to run this application, we will see this error.


So, it will not allow us to initialize more value than maximum value of a data type.

 

Unchecked keyword

Uncheck is just opposite of checked keyword. If we use uncheck then it will not check the resultant range and maximum range of data type. Here is one example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace DesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
           Int32 a = 1234567890;
           Int32 b = 1234567890;
           Int32 c = unchecked(a + b);
           Console.WriteLine("Value of C is= " + c);
           Console.ReadLine();
        }
    }
}


It’s showing negative result, so it does not check range validation.


Conclusion:- 

In this article we have learned how to use checked and unchecked keyword in C# to validate range of data type with value. Hope you have understood the concept.


 

 

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Posted by: Akiii on: 9/23/2013 | Points: 25
Nice article.

Regards,
Akiii
Posted by: Anup1252000 on: 9/23/2013 | Points: 25
Good one. Thanks

Login to post response

Comment using Facebook(Author doesn't get notification)