Checked Block and Casting in c#

Hi, In the below post we will see the use of c# checked block on casting variable in different data types. lets try casting an float value into an int value. below is the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            float floatValue = 999999999999999999999f;
            int intValue = (int)floatValue;
            Console.WriteLine("Float Value : {0}\nInt Value {1}", floatValue, intValue);
            Console.ReadLine();
 
        }
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

and below is the output window of this console program.

image

as you can see that casting worked but the value in the int variable is not expected , also one question in your mind is c# didn’t warned you about it. By default, c# is not going to warn you about this type of casting errors, for knowing and handling these errors c# has a checked block. this block is used for checking the overflow in the arithmetic operations and conversions.

Now lets try the above code with checked block and exception handling block.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                checked
                {
                    float floatValue = 999999999999999999999f;
                    int intValue = (int)floatValue;
                    Console.WriteLine("Float Value : {0}\nInt Value {1}", floatValue, intValue);
                    Console.ReadLine();
                }
            }
            catch (System.OverflowException ex)
            {
                Console.WriteLine("Overflow exception , Message:" + ex.Message);
                Console.ReadLine();
            }
        }
    }
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

and below are the output window when we run this program. 

image

and we caught overflow exception.

Hope you find this post useful , Thanks for reading. 🙂

 

 

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s