Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is casting to an interface a boxing conversion?

Tags:

c#

oop

boxing

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?

like image 884
Hemanshu Bhojak Avatar asked Jun 23 '10 13:06

Hemanshu Bhojak


People also ask

What is Interface casting?

A type cast—or simply a cast— is an explicit indication to convert a value from one data type to another compatible data type. A Java interface contains publicly defined constants and the headers of public methods that a class can define.

What is type casting explain the concept of boxing and unboxing?

C# Type system contains three types they are called Value types, Reference Types and Pointer Types. C# allows us to convert a value type to a reference type, and back again to value type. The operation of converting a value type to a reference type is called Boxing and the reverse operation is called Unboxing.

Which one of the following is implicit boxing?

The process of converting a Value Type variable (char, int etc.) to a Reference Type variable (object) is called Boxing. Boxing is an implicit conversion process in which object type (super type) is used.

How performance is affected due to boxing and unboxing?

During process of boxing and unboxing, it may be possible that the performance of conversion is being affected due the large number of items stored in a collection. I've done a bit of research over this and here is my conclusion. As a general conclusion, we can say that an object is equivalent to a 'void *' of c++.


1 Answers

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.
like image 161
drwatsoncode Avatar answered Nov 09 '22 23:11

drwatsoncode