Making sure unsigned int/long always execute in checked context in C#

Posted by theburningmonk on Stack Overflow See other posts from Stack Overflow or by theburningmonk
Published on 2010-05-05T10:33:28Z Indexed on 2010/05/05 10:38 UTC
Read the original article Hit count: 261

Filed under:
|
|
|

Has anyone found it strange that the default context for uint and ulong is unchecked rather than checked considering that they are meant to represent values that can never be negative?

So if some code is trying to violate that constraint it seems to me the natural and preferred behaviour would be to throw an exception rather than returning the max value instead (which can easily leave important pieces of data in an invalid state and impossible to revert..).

Is there an existing attribute which can be applied to either class/assembly so that it always performs arithmetic operations in a checked context? I was thinking of writing one myself (as an aspect using PostSharp) but would be great if there's one already.

Many thanks,

© Stack Overflow or respective owner

Related posts about c#

Related posts about unsigned