2

I'm currently trying to get my head around casting and boxing. As i understand it currently:

  • Boxing - Value Type to Reference Type (ie int to object)
  • Unboxing - Reference Type to Value Type (ie object to int)
  • Type Casting - Seems to me at the moment to be similar to boxing but allows you to assign what type of object you would like the Reference Type to be of. (ie int to customObjectType)

The example im working with at the moment to try and get my around it. Say I have 2 classes, a method in one class calls the constructor of the other.

//1st class
public class FirstClass
{
  //code for fields,constructor & other methods

  public void CallOtherClass(int firstClassID)
  {
   int x = firstClassID;
   SecondClass test = new SecondClass(x);
  }

}

//2nd class
public class SecondClass
{
   public SecondClass(FirstClass firstClass)
   {
    //set some fields
   }
} 

Ok, so in the above scenario we would have a problem as the method CallOtherClass attempts to set the constructor of SecondClass, however the constructor of SecondClass takes a parameter of type FirstClass and all we can provide is an int.

So as i understand it this would be a good time to use type casting? Something like below.

//1st class
public class FirstClass
{
  //code for fields,constructor & other methods

  public void CallOtherClass(int firstClassID)
  {
   int x = firstClassID;

   FirstClass a;
   a = (FirstClass)x;

   SecondClass test = new SecondClass(a);
  }

}

//2nd class
public class SecondClass
{
   public SecondClass(FirstClass firstClass)
   {
    //set some fields
   }
}

In my head this seems to me to change the type of x to a reference type of FirstClass. Obviously my understanding is way off some where along the lines as it produces an error

"Cannot convert type 'int' to 'Namespace.FirstClass"

Any thoughts?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
cosmicsafari
  • 3,949
  • 11
  • 37
  • 56
  • How would you cast an 'int' to become an instance of a class? That's what you're trying to do there. – George Stocker Nov 07 '12 at 01:35
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Nov 07 '12 at 02:06
  • Related post - [What is the difference between boxing/unboxing and type casting?](https://stackoverflow.com/q/1085144/465053) – RBT May 01 '18 at 06:53

1 Answers1

2

Type casting isn't boxing or unboxing, but it may cause either. As an int isn't a FirstClass, that is, int doesn't inherit or extend FirstClass, hence you can't cast it to be of type FirstClass.

Typecasting causes conversion, only if it is possible to do so. So you can go from an int to a double and vice versa, with possible side effects. But you can't go from an int to FirstClass.

Boxing, wraps a value or reference type in wrapper object. At least that's how I think of it. Not sure how the internals work, but my guess is the assignment operator "=" or casting implicitly returns the wrapped value when unboxing, and the wrapper object when boxing.

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
  • Thanks, that shed some light on the differences for me. So in the above scenario boxing would be the thing to do rather than casting as SecondClass isn't a derived from FirstClass. – cosmicsafari Nov 07 '12 at 08:52
  • In the first scenario you were never casting SecondClass to FirstClass, but x which is of type int. So yeah you could use boxing and have have a field `private int x` and store data into it. You'd have to do more work though. To make it work for casts, initialization, assignment. But the Integer class already does that.. http://msdn.microsoft.com/en-us/library/85w54y0a(v=vs.80).aspx – Lews Therin Nov 07 '12 at 08:57
  • 1
    Thanks, I'll have a look. Just noticed your username, great series of books! – cosmicsafari Nov 07 '12 at 10:09
  • Have fun :P and yep! Can't wait for the AMoL :P – Lews Therin Nov 07 '12 at 10:49