Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different ways of type-conversion. What is the difference

I am trying to get a difference between the type casting methods.

eg.

Method 1

public byte fun()
{
   object value=1;
   return (byte)value; // this gives me error
}

Method 2

public byte fun()
{
   object value=1;
   return byte.Parse(value.ToString()); // this runs 
}

Method 3

public byte fun()
{
   object value=1;
   return Convert.ToByte(value); // this runs
}

What is the difference between all the three. How they are working internally. What are value type and refrence type here. Which function can convert value type to ref type and vice versa

Edit 2

When i writes this line what datatype '1' will be treated by default int32, byte or something else.

object value=1;

like image 633
Shantanu Gupta Avatar asked Jan 23 '23 12:01

Shantanu Gupta


2 Answers

There are a lot of questions here.

Method 1 fails because you cannot do an unbox and a cast in a single operation. You're setting "value" to a boxed integer. When you try to do the cast, you're unboxing the integer and trying to cast to a byte in a single operation, which fails. This does work, btw:

return (byte)( (int)value) ); // Unbox, then cast, in two operations

Method 2 works because you're converting the integer to a string, then using byte.Parse to convert it to a byte. This is very expensive, since it's going to/from strings.

Method 3 works because it sees that the object in value is IConvertible (int), and uses the appropriate conversion operation to convert to byte. This is probably a more efficient way of approaching it, in this case. Since "value" is storing an int, and int supports IConvertible, Convert.ToByte will basically do a null check, then call Convert.ToByte(int), which is quite fast (it does bounds checking, and a direct cast).

I'd recommend reading Eric Lippert's blog post titled Representation and Identity. It covers casting in detail, and explains why method 1 fails...

like image 172
Reed Copsey Avatar answered Feb 02 '23 09:02

Reed Copsey


// This is a direct cast. It expects that the object
// in question is already allocated as the data type
// indicated in the cast
(byte)value;

// This is a direct code conversion. It takes the argument
// and runs through code to create a new variable of the
// type byte. You'll notice if you include this in different
// code that value will still be an object but your new
// data will be a byte type
byte.Parse(value.ToString());

// This will convert any object similarly to the byte.Parse.
// It is not as fast because it does not have a definitely
// typed parameter (as parse has string). So it must go 
// through a couple of extra steps to guarantee the conversion
// is smooth.
Convert.ToByte(value);

Direct casting is always the fastest. It assumes the type is already established and allocated so all it has to do is switch its reference type in memory. The conversion methods are code conversions so they require a little more time. I don't know the benchmarks, but Parse is slightly faster because it deals with a specific in and a specific out (string->byte). Convert is the slowest of the conversion methods because it lacks this same specificity.

like image 27
Joel Etherton Avatar answered Feb 02 '23 08:02

Joel Etherton