Absolutely no implicit casting allowed!

G

Gecko

I would like to know if there is a way to stop the runtime from implicitly
casting values. For exampel, I would like the following code to crash:

byte someByte = 5;
int someInt = someByte;

I would like the assignment of the variable "someByte" to the variable
"someInt" to crash because there is an implicit conversion from Byte to Int
and I DO NOT want that.

I have reasons why I would like this behavior. Any way this can be done?
Thanks
 
J

Joshua Flanagan

I don't know of any way to force the compiler to create an error in that
case, but I bet you could create a custom FxCop rule to analyze your
code and generate errors. You could then incorporate FxCop analysis as
part of your build (using post build events, or a more flexible build
tool like NANT).

John Robbins wrote a couple good articles about writing custom FxCop
rules that you might find helpful. He references them in this article:
http://wintellect.com/WEBLOGS/wintellect/archive/2004/11/09/619.aspx

Hope that helps.

Joshua Flanagan
http://flimflan.com/blog
 
G

Gecko

Thanks Josh, I heard about FxCop some time ago, I actually downloaded it but
have not gotten around to try it. I will definitely be taking a look into it
now.



Thanks again.
 
J

Jon Skeet [C# MVP]

Gecko said:
I would like to know if there is a way to stop the runtime from implicitly
casting values. For exampel, I would like the following code to crash:

byte someByte = 5;
int someInt = someByte;

I would like the assignment of the variable "someByte" to the variable
"someInt" to crash because there is an implicit conversion from Byte to Int
and I DO NOT want that.

I have reasons why I would like this behavior. Any way this can be done?

Without writing your own compiler, you won't be able to force your code
not to compile.

I'm intrigued though - out of interest, why do you want to prevent
implicit conversions?
 
A

Alejandro Lapeyre

There are two types of conversions:

An implicit (widening) coercion shall not lose any magnitude or precision.
These should be
provided using a method named op_Implicit

An explicit (narrowing) coercion may lose magnitude or precision. These
should be provided
using a method named op_Explicit

Byte to integer is a widening convertion and there is an implicit convertion
for that, so I would
limit the crashing to narrowing convertions.



Best Regards,
Alejandro Lapeyre
 
M

Michael C

Alejandro Lapeyre said:
There are two types of conversions:

An implicit (widening) coercion shall not lose any magnitude or precision.
These should be
provided using a method named op_Implicit

An explicit (narrowing) coercion may lose magnitude or precision. These
should be provided
using a method named op_Explicit

Byte to integer is a widening convertion and there is an implicit
convertion for that, so I would
limit the crashing to narrowing convertions.

Isn't that what C# does by default?

Michael
 
R

rossum

I would like to know if there is a way to stop the runtime from implicitly
casting values. For exampel, I would like the following code to crash:

byte someByte = 5;
int someInt = someByte;

I would like the assignment of the variable "someByte" to the variable
"someInt" to crash because there is an implicit conversion from Byte to Int
and I DO NOT want that.

I have reasons why I would like this behavior. Any way this can be done?
Thanks

As others have pointed out, since the cast does not lose either
magnitude or precision, the compiler will do it automatically.

You might try replacing one or other of the types involved with a very
simple struct, so you can define which casts are explicit and which
are implicit. I suggest a struct since your example uses value types
rather than reference types; a class would work equally well.

struct NoCastInt {
int m_value;
}


static void Main() {
byte someByte = 5;
int someInt = someByte; // Compiles OK
NoCastInt someNCInt = someByte; // Compile error
}

rossum


The ultimate truth is that there is no ultimate truth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top