C # - user explicit conversion with checked / unchecked statement

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; // b == 0 

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?

+4
source share
6 answers

Section 14.5.12 of the C # language specification explicitly lists all operators that may be affected by marked and unverified keywords.

Custom operators are not part of this list, so no, you cannot write a custom conversion operator that takes the checkbox / unchecked into account.

+5
source

checked - compilation time. That is, its only effect will be on the code block directly , surrounded by the checked operator, and not the methods called in this block. Therefore, at run time, at run time, there would be no checked and unchecked context, that you can configure the behavior of the function to suit it.

+6
source

checked calls the "check for overflow" command of the corresponding CIL instruction, which must be generated by the compiler.

Here's a dump from IL Disassembler:

 //000012: Int32 i = 42; IL_0001: ldc.i4.s 42 IL_0003: stloc.0 //000013: Byte b1 = (Byte) i; IL_0004: ldloc.0 IL_0005: conv.u1 IL_0006: stloc.1 //000014: Byte b2 = checked ((Byte) i); IL_0007: ldloc.0 IL_0008: conv.ovf.u1 IL_0009: stloc.2 
+4
source

Does your problem solve just doing this:

 class Foo { public static explicit operator int(Foo bar) { unchecked { return some Foo to int conversion } } } 
0
source

As I commented on some of the answers, I think I got the answer I was looking for. As far as I understand, controlling a controlled / unchecked explicit conversion is only possible with "built-in numerical conversions."

Moreover, it seems that uncontrolled transformation is an evil that brings strange results.

0
source

I think this can be done using the preprocessor directive. Just as you can say:

 #if DEBUG Console.WriteLine("This code is only compiled in debug mode"); #else Console.WriteLine("This code is only compiled in release mode"); #endif 

you could say:

 #if CHECKED // code here to be compiled only if "checked" is the default overflow checking context #else // code for "unchecked" #endif 

But see / define (C # compiler options) for details on how to define such a character. It can be defined in the .csproj file.

Important: This approach is only useful to distinguish between a situation where you change the default global overflow check context from a situation where you do not. It is not possible to determine if your explicit operator method was called from another method that used the checked / unchecked keyword because, as others have said, the checked / unchecked keyword no longer works when control enters your method. When entering the method, the default overload checking context is used, as indicated by the compiler settings or the runtime configuration, and that the context that I suggest you can detect using the preprocessor directive.

0
source

All Articles