0

I could use something like this to check...

private static readonly int IntMaxValue = int.Parse(int.MaxValue.ToString());
private static bool IsChecked()
{
    try {
        var i = (IntMaxValue + 1);
        return false;
    }
    catch (OverflowException) {
        return true;
    }
}

... but that's a lot of overhead in a tight loop, throwing and catching just to detect it. Is there a lighter way to do this?

EDIT for more context...

struct NarrowChar
{
    private readonly Byte b;
    public static implicit operator NarrowChar(Char c) => new NarrowChar(c);
    public NarrowChar(Char c)
    {
        if (c > Byte.MaxValue)
            if (IsCheckedContext())
                throw new OverflowException();
            else
                b = 0; // since ideally I don't want to have a non-sensical value
        b = (Byte)c;
    }
}

If the answer is just 'no', don't be afraid to simply say that :)

Nick Strupat
  • 4,928
  • 4
  • 44
  • 56
  • 3
    This has [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) written all over it. What are you _really_ trying to solve? (Never mind what's the point of sending `int.MaxValue` round-trip through a `string` value? Why have that field at all, and why initialize it that way?) – Peter Duniho Feb 06 '17 at 02:37
  • @PeterDuniho the compiler won't let you obviously overflow. I'm just getting around that in the example. – Nick Strupat Feb 06 '17 at 02:44
  • @PeterDuniho I'm implementing a type which I'd like to have the same semantics as the primitive types (in this case it's conversion operators from a larger type) – Nick Strupat Feb 06 '17 at 02:49
  • @PeterDuniho is there a name for "Assuming 'XY Problem' before providing a constructive question or answer"? :P – Nick Strupat Feb 06 '17 at 02:51
  • @NickStrupat I think it's a fair assumption. *Why* do you need to check if you're in a `checked{}` block? The same semantics as primitives is to simply throw an exception if not (which will happen if you're converting a value to a smaller type that cannot store it). Can you please provide an example of your intended usage of `IsChecked`? – Rob Feb 06 '17 at 02:53
  • @Rob I added a small example of what I'm talking about – Nick Strupat Feb 06 '17 at 05:02
  • http://stackoverflow.com/questions/28080009/how-can-i-check-if-im-in-a-checked-context – Nick Strupat Feb 06 '17 at 05:13

1 Answers1

1

So the answer seems to be 'no', but I figured out the solution for my particular problem. It could be useful to someone else who ends up in this situation.

public NarrowChar(Char c) {
    var b = (Byte)c;
    this.b = (c & 255) != c ? (Byte)'?' : b;
}

First we "probe" the checked/unchecked context by just trying the cast. If we're checked, the overflow exception is thrown by the (Byte) c. If we're unchecked, the bit mask and comparison to c tells us if there was an overflow in the casting. In our particular case, we want the semantics of NarrowChar such that a Char that won't fit in a Byte gets set to ?; just like if you transcode a String of to ISO-8759-1 or ASCII you get ?.

Doing the cast first is important to the semantics. Inlining b will break that "probing" behaviour.

Nick Strupat
  • 4,928
  • 4
  • 44
  • 56