.NET interview questions: – How can you explain uses of checked and unchecked keyword in C#?

Checked: -The checked keyword is used to explicitly enable overflow
checking for integral-type arithmetic operations and conversions.

Unchecked: – The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions.

Now, let’s create a small console application and try to demonstrate the use of both the keywords (checked and unchecked).

Now, for demonstration purpose let’s take “int” data type and see what the maximum value it can hold.

Add the below code snippet in to your application to known the maximum value can be hold by the int data type.

class Program
{
static void Main(string[] args)
{
Console.WriteLine(int.MaxValue);
Console.ReadLine();
}
}

The below diagram shows the exact value can be hold by “int” data
type.

Now, let’s see where the checked keyword can help us in making your code more useful.

class Program
{
static void Main(string[] args)
{
int x = 2147483647;
int y = 2147483647;

int z = x + y;
}
}

In the above code snippet you can see that I have tried to add extra value to the “z” variable which is of “int” data type and it can hold only “2147483647” value, now let’s see what happen when we try to do that.

This is somewhat we were not expecting your expectation was the compiler should throw some error (overflow) or exception.

Checked keyword helps us to achieve this like below code snippet.

class Program
{
static void Main(string[] args)
{
int x = 2147483647;
int y = 2147483647;

int z = checked(x + y);
}
}

Now, when you run your application the compiler will raise an error like below diagram.

In simple words the checked keyword is used in scenarios where you want to ensure that your left hand side data type is not getting overflow.

Now, let’s see what the use of unchecked keyword is.

Note: In simple words the unchecked keyword behaves almost the same way as the default behavior of the compiler.

Let’s prove that the above point by a simple demonstration.

class Program
{
static void Main(string[] args)
{
int x = 2147483647;
int y = 2147483647;

int z = unchecked(x + y);
}
}

In above code snippet you can see that I have just added the unchecked keyword in front of the arithmetic expression of the z variable.

Now, just run your application by putting a small debug pointer and later just move your mouse pointer to the “z” variable you will find output like below diagram.

So this prove your above point that the unchecked keyword works almost same as the default compiler works, now the question comes here is that when the default compiler works same as unchecked keyword then what is the exact use of it.

Now, let’s see a simple demonstration to know what the exact use of unchecked keyword is.

class Program
{
static void Main(string[] args)
{
const int x = 2147483647;
const int y = 2147483647;

int z = x + y;
}
}

In the above code snippet you can see that I have declared variable x and y as const int. now, let’s try to compile this code and see that what the compiler has to say about it.

I f you want to bypass this behavior then unchecked keyword will help you to achieve this like below code snippet.

class Program
{
static void Main(string[] args)
{
const int x = 2147483647;
const int y = 2147483647;

int z =unchecked(x + y);
}
}

Now, when you compile this code you will see that the compiler doesn’t throw any error like below diagram.

Conclusion:

Use checked keyword when you want to ensure that there is no overflow happening.

Use unchecked keyword when you want to bypass the default behavior of the constant type checking.

Can also view video on use of checked and unchecked keyword in .NET as follows:-


View more interview questions and answers for .NET

Regards,

Visit for more author’s Most asked Dotnet interview questions

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

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