Home  >  Article  >  Backend Development  >  C# checked

C# checked

王林
王林Original
2024-09-03 15:16:511058browse

To explicitly check the overflow for integral type operations and conversions in arithmetic, the overflow checking must be enabled for integral type operations and conversions in arithmetic and this is possible by making use of a keyword called checked keyword in C# and by using checked keyword for expression in C#, the constant expression is checked for overflow during run time and if there is overflow, overflow exceptions are raised by the compiler whereas the expressions those are non-constant are not checked for overflow during the run time despite the use of checked keyword and overflow, exceptions are not raised in such cases.

 The syntax of checked keyword C# is as follows:

checked(Expression)

Working of the checked keyword in C#

  • Whenever a number is overflown, the value of the number becomes invalid. As a result, no exceptions will be thrown.
  • For the exceptions to be thrown whenever a number is overflowing, we make use of the keyword checked.
  • Whenever a checked keyword is used for an expression whose value is overflowing, exceptions will be raised.
  • Since there are exceptions that will be raised for overflowing values, the errors can be avoided by catching the exceptions. As a result, the quality of the resulting program will be good.
  • Consider the below program to demonstrate the use of checked keyword in a C# program:

Code:

//a namespace called check is defined
namespace check
{
//a class called space is defined
public class space
{
//main method is called
public static void Main()
{
// Using checked keyword to check if the value is over flowing
checked
{
// Increasing the value upto the value of maximum
int number = 0;
for (int x = 0; x < int.MaxValue; x++)
{
number++;
}
// Increasing the value upto the value of maximum again (error).
for (int x = 0; x < int.MaxValue; x++)
{
number++;
}
}
}
}
}

Output:

C# checked

In the above program, a namespace called check is defined. Then a class called space is defined. Then the main method is called. Then we have used checked keyword to check if the value is overflowing by increasing the value up to the value of maximum. If we do not make use of the checked keyword in the above program, no exception will be thrown. Because we have made use of checked keyword in our program, an exception as shown in the output snapshot is thrown. The checked keyword in the program can be activated by selecting the option in the compiler “/checked”. An overflow exception is raised when the variable number is incremented by one every time to reach the maximum value of the integer which cannot be fit in the memory location. Overflow exception is raised during the run time by using special intermediate instructions.

Examples of C# checked

Here are the following examples mention below

Example #1

C# program to demonstrate the use of a checked keyword to throw exceptions.

Code:

using System;
//a namespace called check is defined
namespace check
{
//a class called space is defined
public class space
{
//main method is called
public static void Main()
{
// Using checked keyword to check if the value is over flowing
checked
{
int twenty = 20;
int j = 2147483647 + twenty;
Console.WriteLine(j);
}
}
}
}

Output:

C# checked

In the above program, a namespace called check is defined. Then a class called space is defined. Then the main method is called. Then we have used checked keyword to check if the value is overflowing by assigning the value 20 to a variable twenty and then adding it to the maximum value of the integer so that an exception will be thrown. If we do not make use of the checked keyword in the above program, no exception will be thrown. Because we have made use of checked keyword in our program, an exception as shown in the output snapshot is thrown. The checked keyword in the program can be activated by selecting the option in the compiler “/checked”. An overflow exception is raised when the value of the variable twenty is added to the maximum value of the integer which cannot be fit in the memory location. Overflow exception is raised during the run time by using special intermediate instructions.

Example #2

C# program to demonstrate the use of a checked keyword to throw exceptions.

Code:

using System;
//a class called check is defined
public class Check
{
//the maximum value of integer is assigned to a variable
static int maxInt = 2147483647;
//main method is called
public static void Main()
{
Console.WriteLine(CheckMethod());
}
static int CheckMethod()
{
int y = 0;
try
{
y = checked (maxInt + 1);
}
catch (System.OverflowException e)
{
Console.WriteLine("An overflow exception is raised because of maximum integer value " + e.ToString());
}
return y;
}
}

Output:

C# checked

In the above program, a class called Check is defined. Then the maximum value of the integer is assigned to a variable maxInt. Then the main method is called. Then we call the check method function within which we have made use of the try-catch block which consists of checked keyword usage to add one to the maximum value of the integer and the catch block consists of the exception statement we want to be displayed. If we do not make use of the checked keyword in the above program, no exception will be thrown. Because we have made use of checked keyword in our program, an exception as shown in the output snapshot is thrown. The checked keyword in the program can be activated by selecting the option in the compiler “/checked”. An overflow exception is raised when the value of the variable twenty is added to the maximum value of the integer which cannot be fit in the memory location. Overflow exception is raised during the run time by using special intermediate instructions.

Conclusion

In this tutorial, we understand the concept of the checked keyword in C# through definition, the syntax of the checked keyword in C#, Working of the checked keyword in C# through examples, and their outputs.

The above is the detailed content of C# checked. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# BufferNext article:C# Buffer