Is casting to an interface a boxing conversion?
Asked Answered
T

7

14

I have an interface IEntity

public interface IEntity{
    bool Validate();
}

And I have a class Employee which implements this interface

public class Employee : IEntity{
    public bool Validate(){ return true; }
}

Now if I have the following code

Employee emp1 = new Employee();
IEntity ent1 = (IEntity)emp1; // Is this a boxing conversion?

If it is not a boxing conversion then how does the cast work?

Tripitaka answered 23/6, 2010 at 13:14 Comment(1)
In this case, the cast is not required. Employee implements the IEntity interface, so you can freely assign a reference to IEntity (ent1 in your example) to any reference of Employee (again since it implements the IEntity interface) without requiring an explicit cast.Centerpiece
E
19

No, since Employee is a class, which is a reference type rather than a value type.

From MSDN:

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object.

The aforementioned MSDN link has further examples that should help clarify the topic.

Edna answered 23/6, 2010 at 13:19 Comment(1)
Probably worth clarifying the fact that although it isn't in the actual example the OP shows, it may be in a different situation.Inhume
I
9

In your example above, no but sometimes yes.

Boxing is the process of "boxing" a value type into a referenceable object; a reference type. In your example above, Employee is already a reference type, so it is not boxed when you cast it to IEntity.

However, had Employee been a value type, such as a struct (instead of a class), then yes.

Inhume answered 23/6, 2010 at 13:17 Comment(0)
M
6

As others have said, casting a reference type to an interface is NOT an example of boxing, but casting a Value Type to an interface IS.

public interface IEntity {
    bool Validate();
}

public class EmployeeClass : IEntity {
    public bool Validate() { return true; }
}

public struct EmployeeStruct : IEntity {
    public bool Validate() { return true; }
}


//Boxing: A NEW reference is created on the heap to hold the struct's memory. 
//The struct's instance fields are copied into the heap.
IEntity emp2 = new EmployeeStruct(); //boxing

//Not considered boxing: EmployeeClass is already a reference type, and so is always created on the heap. 
//No additional memory copying occurs.
IEntity emp1 = new EmployeeClass(); //NOT boxing

//unboxing: Instance fields are copied from the heap into the struct. 
var empStruct = (EmployeeStruct)emp2;
//empStruct now contains a full shallow copy of the instance on the heap.


//no unboxing. Instance fields are NOT copied. 
var empClass = (EmployeeClass)emp2; //NOT unboxing.
//empClass now points to the instance on the heap.
Mair answered 12/1, 2013 at 1:33 Comment(1)
Thanks for the examples! Helped a lot!Bullock
C
2

No.

Because emp1 is a reference type.

Boxing occurs when a value type is converted to an object, or an interface type.

Centerpiece answered 23/6, 2010 at 13:18 Comment(0)
I
0

No, it's not.

Your instance of Employee is already a Reference Type. Reference Types are stored on the Heap so it doesn't need to be boxed/unboxed.

Boxing only occurs when you store a Value Type on the Heap or, in MSDN language, you could say:

Boxing is an implicit conversion of a Value Types (C# Reference) to the type object or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object.

Inland answered 23/6, 2010 at 13:17 Comment(0)
T
0

No, boxing occurs when you convert a value type to an object.

Tva answered 23/6, 2010 at 13:17 Comment(1)
I think you mean to have a comma ',' after the first word in your brief reply. Without this--that is, as your reply currently stands--it has the opposite meaning, and is thus incorrect.Tilley
P
0

Boxing means converting a value type to object. You are converting a reference type to another reference type, so this is not a boxing conversion.

Pelt answered 23/6, 2010 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.