I know that this was a discussion on code optimisation and the technology
behind .NET, but I think that explicit casting in this case is a good idea
as a matter of principle.
Performing math on a byte is an exceptional affair, so casting makes you
look carefully at code that could very quickly run into trouble. Better yet,
it has the added benefit of creating self-documenting code - you'd have to
be a real ninny not to know why the code's author was casting a byte to and
from an int.
IMHO explicit casting makes the value's severe limitation obvious to other
programmers maintaining your code (and to yourself in five years when you
forgot what you were doing!)
Carlo
"Vadym Stetsyak" <va*****@ukr.net> wrote in message
news:ud**************@TK2MSFTNGP15.phx.gbl...
Hello, moondaddy!
m> byte val1 = 10;
m> byte val2 = 23;
m> byte ttl;
m> ttl = val1 + val2; //this line wont compile
m> compile error: Cannot implicitly convert type 'int' to 'byte'.
m> val1 + val2 = 33 which is still in the range of a byte. therefore, why
m> do I need to convert ttl into an int?
IMO the trouble here is that IL opcode 'add' doesn't use byte ( int8 )
type to perform additions. That is why the result value is int32.
if you will write
ttl = (byte)(val1 + val2)
then 'conv.u1' opcode is added that converts the value to neede type (
int8 or byte )...
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com