Different types conversion methods. What is the difference

I am trying to get the difference between 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 these three. How they work domestically. What is the type of value and the type of refrence. Which function can convert a value type to a ref type and vice versa

Edit 2

When I write this line, the data type '1' will be processed by default int32, byte or something else.

object value = 1;

+4
source share
3 answers

There are a lot of questions.

Method 1 fails because you cannot do unbox and cast in one operation. You set the "value" to a boxed integer. When you try to perform a throw, you release an integer and try to pass it in bytes in one operation, which fails. This works by the way:

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

Method 2 works because you convert an integer to a string, and then use byte.Parse to convert it to byte. This is very expensive since it is going to / from strings.

Method 3 works because it sees that the object in the value is IConvertible (int) and uses the appropriate conversion operation to convert to byte. This is probably a more effective way to get closer to this, in this case. Since "value" stores int, and int supports IConvertible, Convert.ToByte will basically do a null check and then call Convert.ToByte (int), which is pretty fast (it restricts validation and direct listing).

I would recommend reading Eric Lippert's blog entry, Presentation and Identification . It describes casting in detail and explains why method 1 fails ...

+8
source
 // 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 is assumed that the type is already installed and distributed, so all he needs to do is switch its reference type in memory. Conversion methods are code transformations, so they take a little longer. I do not know the tests, but Parse is a little faster because it deals with a specific and specific byte (string-> byte). Conversion is the slowest conversion method because it lacks the same specificity.

+1
source

Well here is my picture:

Method 1

This is a type, that is, the value must be of a type, which can be in an explicit or explicit conversion to bytes. In addition, the value must not go beyond the byte.

The call fails because the compiler does not have any information about what type of object it should use, and therefore cannot perform an implicit or explicit conversion. Performance

 int obj = 1; byte b = (byte) obj; 

or

 byte b = (byte) (int) obj; 

works. The second option uses expack unboxing (thus providing the necessary information), as described in a comment and publication by Reed Copsey. The link provided by Reed Copsi's comment explains this in detail.

For sampling user objects, the use of implicit and explicit conversions are statements that are static methods defined in the class. For object there are no explicit or explicit operations (see the link for the reason why), while for int these operations exist.

Method 2

Here you parse the string, the string value must be a number that is inside the byte boundary. Here you can also use TryParse , which allows you to check if the conversion is successful.

Method 3

Uses the type conversion of the Convert class. This is the most flexible method supporting most common types. Here, the value must be converted to a number, and the value must be inside the byte bounds. The Convert class uses IConvertible to convert between different types and is therefore extensible.

0
source

Source: https://habr.com/ru/post/1302394/


All Articles