Joan Venge
Joan Venge

Reputation: 331280

Deserialization requires casting?

I was reading this article, where they have this code:

// Serialization
XmlSerializer s = new XmlSerializer( typeof( ShoppingList ) );
TextWriter w = new StreamWriter( @"c:\list.xml" );
s.Serialize( w, myList );
w.Close();

// Deserialization
ShoppingList newList;
TextReader r = new StreamReader( "list.xml" );
newList = (ShoppingList)s.Deserialize( r );

Is the last line a cast statement? If so, doesn't it degrade the serialization performance?

Upvotes: 1

Views: 2249

Answers (5)

Jon Skeet
Jon Skeet

Reputation: 1502406

Casts are hugely cheap compared with the deserialization cost itself. The process of deserialization is pretty complex - a single (working) cast hardly takes any time at all.

Of course, if you're interested in fast, portable, compact serialization with a good versioning story, you should be looking at Protocol Buffers:

(There are other serialization frameworks too, such as Thrift.)

Upvotes: 3

Dave L
Dave L

Reputation: 1634

If you change that last line of code

newList = (ShoppingList)s.Deserialize( r );

to

newList = s.Deserialize( r );

The compiler will add back in a cast. I just confirmed this with Red Gate's .NET Reflector. So regardless of the cost of casting, you are required to do it if you want to use that typed object.

Upvotes: 0

JaredPar
JaredPar

Reputation: 755179

Yes the last line is a cast statement. Casting does have a cost associated with it but it is insignificant as compared to the cost of serialization. I doubt it would even show up on a profiler.

Think of what serialization involves.

  • Processing a byte stream
  • Creating types based on metadata information
  • Conversion between byte arrays and data types

Any of these operations are significantly more expensive than a single cast operation.

EDIT As to why it requires casting at all.

There are a couple of reasons here. The first is that the deserialization APIs have no way of knowing what the type of the byte stream is before it inspects it. So the only choice the API has in terms of a return type in metadata is Object.

Secondly, deserialization must support literally any type that is serializable. In order to function it must pick a return type for the method that is applicable to all types which can be serialized. The only type available that meets that is object.

Upvotes: 6

andleer
andleer

Reputation: 22578

The Deserialize() method returns an object and must be "cast" to the correct type.

Casting is primarily telling the compiler that you know what the object type is since the compiler is unable to infer its type. The runtime will still generate an InvalidCast exception if the type is not what you specified (or a sub-type of the type specified).

The actual cost of casting is minimal.

Upvotes: 1

James Black
James Black

Reputation: 41858

Deserialize returns a type Object so the casting is to get it into the correct class.

Whether is would have any impact on the deserialization or not, you want it to be part of ShoppingList.

Upvotes: 0

Related Questions