I am involved in writing personalized type conversions in C #, and I have a question that I cannot solve with the published Google / MSDN / SO elements before.
Typically, a C # program that narrows a number type does this with an unchecked explicit conversion, for example:
int i = 256; byte b = (byte)i;
however, the following overflow exception:
byte b = checked((byte)i);
My question is this: the behavior of a verified / unverified keyword is implemented when converting a user type, for example:
class Foo { public static explicit operator int(Foo bar) { if (checked) throw someEception else return some Foo to int conversion } }
Of course, the above code is not the answer, but does anyone know if something like this is possible?
source share