Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the [Serializable] attribute do that ISerializable doesn't? [duplicate]

Possible Duplicate:
What's the difference between using the Serializable attribute & implementing ISerializable?

What does tagging a class with Serializable do? e.g.:

[Serializable]
public Hashtable 
{
}

How is it different from the class implementing ISerializable? e.g.:

public Hashtable : ISerializable
{
}

And how is that different from tagging the class as Serializable and implementing ISerializable? e.g.:

[Serializable]
public Hashtable : ISerializable
{
}

What is the purpose of [Serializable] as opposed to ISerializable?

tl;dr: What is [Serializable]?

like image 206
Ian Boyd Avatar asked Dec 03 '22 00:12

Ian Boyd


1 Answers

I thought you'd linked to Serializable, but you didn't:

All the public and private fields in a type that are marked by the SerializableAttribute are serialized by default, unless the type implements the ISerializable interface to override the serialization process.

and,

Apply the SerializableAttribute attribute even if the class also implements the ISerializable interface [...]

That is, the Serializable attribute indicates that this type can be serialized. ISerializable indicates that this type wants to control how this type is serialized.


Or, to put it another way, your question is phrased the wrong way around. SerializableAttribute should always be applied (to serializable classes), and is the "basic" level of serialization. ISerializable adds more (by allowing you to write code to control the process).

like image 52
Damien_The_Unbeliever Avatar answered Dec 24 '22 00:12

Damien_The_Unbeliever