Geoff James Geoff James - 1 year ago 82
C# Question

Cast vs Serialize/Deserialize C#


I have a
method in a base class, which takes an
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)
// ...


EDIT: Just to clarify - I am unable to change the
signature in the base class - unfortunately, I'm stuck with
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

Here's the dictionary:

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

What I have so far...


There are 2 possible ways that I can think of in order to pass
to the

// Pass the myItem directly as an object



// 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);

Consuming the item

Then, I can use 2 possible ways to consume the item I've just passed to
, 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...


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

// consume away...


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
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 Source

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.


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download