Geoff James Geoff James - 10 days ago 6
C# Question

Cast vs Serialize/Deserialize C#

Scenario



I have a
virtual
method in a base class, which takes an
object
as a parameter.

I override this in derived classes - more specifically, view models - in order to work with the different objects from there, also:

public override void SomeMethod(object parameter)
{
// ...

base.SomeMethod(parameter);
}


EDIT: Just to clarify - I am unable to change the
SomeMethod
signature in the base class - unfortunately, I'm stuck with
object
as the type :(

Let's say that each derived class will expect a different type to the next, but I know about what type each class needs to use in order to pass it and consume it.

In particular, I will ask about
Dictionary<string, string>
; but I would be interested to see if this applies to other types.




Imagine this...



So, let's say I have a
Dictionary<string, string>
I want to pass to
SomeMethod()
:

Here's the dictionary:

// create the dictionary
var myItem = new Dictionary<string, string>
{
["Key1"] = "Value1",
["Key2"] = "Value2"
// ...
};





What I have so far...



Possibilities...



There are 2 possible ways that I can think of in order to pass
myItem
to the
SomeMethod()
:

// Pass the myItem directly as an object

SomeMethod(myItem);


or

// Serialize the myItem to a string, and then pass that as an object
// I'm using JsonConvert in this case

object parameter = JsonConvert.SerializeObject(myItem);
SomeMethod(parameter);


Consuming the item



Then, I can use 2 possible ways to consume the item I've just passed to
SomeMethod()
, depending on which method I've used above respectively:

public override void SomeMethod(object parameter)
{
// Cast the parameter back to a dictionary
Dictionary<string, string> unboxedItem = parameter as Dictionary<string, string>;

// consume away...
}


or

public override void SomeMethod(object parameter)
{
// Deserialize the parameter from a string
Dictionary<string, string> deserializedItem =
JsonConvert
.DeserializeObject<Dictionary<string, string>>(parameter as string);

// consume away...
}





Help?



Which of these 2 methods will be less resource-intensive?

I have heard that boxing and unboxing can be end up being slow (in large-scale circumstances).

Yes, I understand that I'm actually boxing the serialized
string
in the second method - does it matter from/to which type I'm boxing/unboxing in terms of resources?


Will casting to/from an object have an impact on performance? Or, will the de/serialization be the real performance-hit?

UPDATE: So, it seems I've been a bit of a silly sausage and misunderstood about the boxing/unboxing stuff - so I've updated the question to reflect this

Many thanks in advance for your comments, suggestions and help :)

Answer

First of all, boxing is not what's happening here. Boxing refers to the operation of preparing value types to be put on the heap as reference types. If your dictionary was a value type, it would indeed need to be boxed and unboxed.

Alas, a dictionary is a reference type. And that means there will be no boxing. The reference will be passed as is to your method. What your method will have to do is a downcast to a dictionary. While this requires the RTTI (run time type information) of your object to be evaluated, the performance impact will usually not be noticed (except in extreme cases).

Your serialization/deserialization solution however is much more expensive. You will have to allocate space for a string in the heap, you have to serialize, deserialize and the string will eventually need to be garbage collected. You can expect that to be far more expensive than a simple downcast.

UPDATE:

Just to make that clear, you can still expect boxing to be cheaper than serialization. So even for value types there is no reason chose serialization. Especially since there is a pretty high chance you will have at least one boxing operation in the whole serialization round trip.