[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Question about Serialization.

I have a question to the sample code in MSDN regarding Serialization:

In the code:
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, addresses);

But the Serialize method is a virtual overloaded method. And I couldn't find the real implementation of the Serialize().

So where is the real implementation of the Serialize() method?

I think my question came from not knowing level details of the Serialization. Anybody can clarify this? Many thanks!
3 Solutions
I'm not quite sure I understand what your question is.  You're saying the BinaryFormatter.Serialize() method is overloaded?  That just means there is more than one way to call the method (more than one acceptable signature).  Either one of them is the "real implementation" of the Serialize() method.  The BinaryFormatter class implements two interfaces: IRemotingFormatter and IFormatter.  The specifications for these interfaces require the class to implement a Serialize() method.
The implementation of the Serialize method is in the BinaryFormatter class. It uses the binary presentation of the argument to serialize it in the underlying stream. The objects to be serialized must always be marked with the [Serializable] attribute.
To add to TheAvenger's comment. As an option You can choose how you want to serialize your class. For that you need to implement ISerializable interface.

One more thing. The virtual method means it will(should) have default implementation however you can give your own implementation is you want to do that. so BinaryFormatter already has the implementation but if u want to use your own formater you can override Serialize method.

Nachiket Shah

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now