On Wed, 15 Jun 2005 20:33:51 -0500, "Gecko" <na**@nada.com> wrote:
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