about understanding serialization and the serializable attribute

T

Tony Johansson

Hi!

Assume I have a collection of Person object in generic class list List<T>.
List<Person> myList = new List<Person();
When I want to serialize this collection myList the Person class must have
the serializable attribute.
I know that the List<T> class also has this serializable attribute otherwise
it will not work.

Now to my question what I store is bunch of Persons objects stored in a
collection List<T> but why
must this List<T> class has this serializable attribute.
I don't fully understand that. I can easily understand that the Person class
must have this serializable attribute.

//Tony
 
P

Peter Duniho

Tony said:
[...]
Now to my question what I store is bunch of Persons objects stored in a
collection List<T> but why
must this List<T> class has this serializable attribute.
I don't fully understand that. I can easily understand that the Person class
must have this serializable attribute.

If List<T> was not serializable, how would you expect to serialize it?
 
T

Tony Johansson

Peter Duniho said:
Tony said:
[...]
Now to my question what I store is bunch of Persons objects stored in a
collection List<T> but why
must this List<T> class has this serializable attribute.
I don't fully understand that. I can easily understand that the Person
class must have this serializable attribute.

If List<T> was not serializable, how would you expect to serialize it?

One more thing is it any advantage to use one over the other between these
two.
IFormatter something = new BinaryFormatter();
or
BinaryFormatter something = new BinaryFormatter();

//Tony
 
P

Peter Duniho

Tony said:
One more thing is it any advantage to use one over the other between these
two.
IFormatter something = new BinaryFormatter();
or
BinaryFormatter something = new BinaryFormatter();

It depends on what you do with it after. If you only ever using the
variable declared as "BinaryFormatter" in places where you really only
need a variable declared as an "IFormatter", then no…there's no
difference. Otherwise, there's a difference.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top