Tim Tim - 2 months ago 13
C# Question

Does casting to base class involve boxing and unboxing

Having below classes:

public abstract class FieldElementBaseCL : FormElementBaseCL
{
public FieldElementBaseCL(FormElementGroupBaseCL parentElement, int linkedFieldID) : base(parentElement)
{
LinkedFieldID = linkedFieldID;
}

[DataMember]
[Browsable(false)]
public int LinkedFieldID { get; set; }

[Browsable(false)]
public LabelControl LabelComponent { get; set; }

public abstract FieldElementDTOBase GetElementDTO();

}

[DataContract]
public class FieldElementNumberEditCL : FieldElementBaseCL
{
public FieldElementNumberEditCL(FormElementGroupBaseCL parentElement, int linkedFieldID) : base(parentElement, linkedFieldID)
{

}

public override FieldElementDTOBase GetElementDTO()
{
FieldElementNumberEditDTOCL elementDTO = new FieldElementNumberEditDTOCL();

return elementDTO;
}
}


As you see, there is line which creates instance of
FieldElementNumberEditCL
class. So, does
GetElementDTO
involve boxing/unboxing as its return type is
FieldElementBaseCL
?

Answer

Boxing and unboxing is only applicable to value types. Value types are those declared with struct. You are not using struct, you are using class. Therefore, boxing/unboxing is completely inapplicable to you.