detecting checked and/or unchecked state in operation overloads RRS feed

  • Question

  • All,

      I wrote some custom value data types and wrote my own +, -, /, *, ^, and % operators.  I want to know if there is anyway to detect if someone using my data type has specified the "checked" and/or "unchecked" statement at run-time (see example below).  Is this possible in .NET? 

    MyDataType a = new MyDataType("100000000000000000000000");
    MyDataType b = new MyDataType("100000000000000000000000");
    // current implementation requires following line to prevent
    // OverflowException.
    //     MyDataType.IsChecked = false;
    MyDataType c = unchecked(a * b); // no exception should be thrown
    c = checked(a * b); // throws OverflowException internally
    • Edited by Rashad Rivera Friday, August 21, 2009 2:43 AM Code block formatting issue
    Friday, August 21, 2009 2:39 AM


  • That's not possible, un/checked can only affect the predefined operators.  Section 7.5.12 of the C# language specification.
    Hans Passant.
    Friday, August 21, 2009 3:34 AM