What exactly happens when I am casting reference types and value types

What exactly happens when I cast by reference types and value types and vice versa (boxing and un boxing) at the compiler or run level? Can any body explain the following four conditions? Please feel free to add terms if I skip any.

 1. Stream stream = new MemoryStream();
    MemoryStream memoryStream = (MemoryStream) stream;
 2. double k=10.0;
    int l = (int)k;
 3. object k =20;
    int l = (int)k;
 4. int k =23;
    double m = k;
+3
source share
1 answer

Three types of conversions take place here:

1 . CLR , stream MemoryStream ( ), MemoryStream. - . stream MemoryStream . .

2 4 - . FPU. 2 ( ), 4 , , .

3 unboxing: CLR , k int ( , int), l.

IL, , , , , , ildasm Reflector.

, .

+4

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


All Articles