Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if I'm in a checked context?

Tags:

c#

.net

math

How can I find out, using C# code, if I'm in a checked context or not, without causing/catching an OverflowException, with the performance penalty that incurs?

like image 226
Evgeniy Berezovsky Avatar asked Jan 22 '15 01:01

Evgeniy Berezovsky


People also ask

How to use checked in if condition in JavaScript?

None of the answers caught this issue. checked is a boolean property, so you can directly use it in an if condition <script type="text/javascript"> function validate () { if (document.getElementById ('remember').checked) { alert ("checked"); } else { alert ("You didn't check it!

How to acknowledge the selected menu item by using the check mark?

This support will help users to easily acknowledge the selected menu item by using the check mark. The Checked property indicates whether a check mark should appear before the text of the menu item or not. The CheckState property specifies the exact state - checked or unchecked which needs to be set either statically.

How do I verify the proxy settings of the machine context?

In Windows Vista and Windows Server Codename Longhorn, use netsh winhttp show proxy to verify the proxy settings of the machine context. If you have a certificate and want to verify its validity, perform the following command:

What is the difference between the checked and checkstate property?

The Checked property indicates whether a check mark should appear before the text of the menu item or not. The CheckState property specifies the exact state - checked or unchecked which needs to be set either statically. On runtime, user need to toggle the state manually through the Click event of the menu item.


1 Answers

The only difference between a block that is checked vs unchecked are the IL instructions generated by the compiler for basic value type arithmetic operations. In other words, there is no observable difference between the following:

checked {
  myType.CallSomeMethod();
}

and

myType.CallSomeMethod();

But lets say that there is an arithmetic operation, such as adding two integers. You would need to get the IL instructions for the method and check if the instructions around your method call are checked, and even that is far from bullet proof. You cannot tell if your custom operation is actually within the checked block, or just surrounded by checked blocks that it is not inside.

Even catching an exception will not work, since you cannot differentiate between these two cases:

checked {
  int a = (Some expression that overflows);
  myType.CallSomeMethod();
}

and

checked {
  int a = (Some expression that overflows);
}
myType.CallSomeMethod();

This is probably part of why the Decimal type does not attempt to detect checked vs unchecked and instead always throws OverflowException.

like image 73
Chris Pitman Avatar answered Oct 21 '22 00:10

Chris Pitman