views:

498

answers:

8

What is a serializable object in C#? I guess the word serializable is throwing me off more than "serializable object".

+4  A: 

An object that can be converted to bits and stored on a medium, such as a hard drive. http://en.wikipedia.org/wiki/Serialization

Cinder6
can you provide an example of an object that would NOT be serializable?
Technically, any object that's not been serialized through some language construct or methodology is not serializable. If you want an example of an object you typically wouldn't serialize, then the general idea is "anything you don't need/want to save". In the MVC design pattern, you wouldn't typically serialize the view, as the controller should be instructing the view what to do (so you would serialize the model and the controller).
Cinder6
As an example of what is _not_ serialisable, consider something that will be very difficult, if not impossible, to reconstruct on a machine that doesn't have the same environment.For example, ResultSets aren't serialisable, because they can hold connections back to the database.
Noon Silk
A: 

You can mark an object as [serializable] in C#, which mean that it can be converted to binary, SOAP, XML, in .net anyhow.

The beauty of this is that you can serialize an object send it across the internet, network etc then reinstate it on the other side as an object again. This can then cross machine boundaries, such as a windows machine to a Unix machine as long as the Computer on the other side is able to read the data and de-serialize it.

See this article: http://www.devhood.com/Tutorials/tutorial%5Fdetails.aspx?tutorial%5Fid=236

StarSignLeo
+1  A: 

Serializing in general means to save an objects state into a 'saveable' format (like saving to disk) so that it can be deserialized later on into an actual object. It is usually done to also send an object over the network in case of remote calls. If you dont want save and also if you dont want to send an object over the wire you can ignore the serializable part (in Java you dont implement the Serializable interface)

OpenSource
+7  A: 

Normally objects are random access, that is, you can specify any part of an object (property or field) and access that part directly. That's all well and fine if you're using RAM to store an object, because RAM is Random Acess Memory and is therefore suited to the job.

When you need to store your object on a medium that is not traditionally random access, for instance disk, or you need to transfer an object over a stream medium (such as the network) then the object needs to be converted into a form that is suitable to the relevant medium. This conversion process is called serialization, because the structured object is flattened or serialized, making it more amenable to being stored for the long term, or transferred over the network.

Why not just copy the bits comprising the object in RAM to disk, or send it as an opaque blob over the network? ... you may ask. A few issues:

  1. Often the format that the object is stored in memory is proprietary and therefore not suitable for public consumption--the way in which it is stored in memory is optimised for in-memory use.
  2. When an object references other objects, those references only have meaning within the context of the running application. It would not be possible to deserialize the object meaningfully unless during the serialization process, the object graph was walked and serialized accordingly. There may be a need to translate those references into a form that has meaning outside the context of an application instance.
  3. There may be an interoperation requirement between heterogenous systems, in which case a standard means of representing the object is required (typically some form of XML is chosen for this).
Eric Smith
+1  A: 

Object serialization is storing the instance's state so you can reconstruct that instance again later.

In most (C# and Java), a serializable object is "marked". In Java you need to implement Serializable. In C# you need to use [Serializable].

Once the object is serialized you can store it in a file or send it over the network.

Think of it like going through every instance variable of an instance and storing its value, separated by some separator (although, it's a lot more sophisticated than that; think of what happens if you have instance variables of non-primitive types, you're gonna have to store all the values inside those, too).

One use of it would be saving a game.

Hope that helped, God bless.

Leo Jweda
A: 

Serialization :its a technique to convert object into binary format,simple object access protocol(SOAP) , or xml documents that can be easily stored ,transfered and retrieved .

In simple way serialization way that we can compress and decompress the data and transfer the data across network in a secure way.

object serialization is what ljuwaidah explained . Try this link also link text

anishmarokey
A: 

In addition to what has been said, I think it's important to mention that serialization of data implies giving it a well defined order (serial comes from series, which means having something lined up or in line).

For instance, serializing a graph (e.g. an RDF graph as known from the "semantic web") into a serialization format such as XML means that there must be a ruleset defining how to put the information contained in the graph into an order, so that it can later be reconstructed by applying the reverse serialization rule (deserializing it).

Matthias