Several answers explain the reasons why you might want to use serialization in general. You seem to also want to know why a particular class has a [Serializable]
attribute, and you wonder why this is possible.
In ASP.NET, the default session state store is InProc
, which allows you to store any object as a link and leave it on the heap. This is the most efficient way to store session state, but it only works if you use one workflow or if all session state can be automatically restored if the workflow should change (unlikely). For other state storage modes ( StateServer
and SQL Server
), all session state objects must be serializable because the ASP.NET engine first serializes these objects using binary serialization before sending it to the media.
In your case, you can use InProc
. One of the reasons is that you still mark all classes that are used in the session state as Serializable
and check them so that you may need to change this in the future (for example, to use a web farm). If you are not developing your session state classes, keeping in mind that in the future it will be quite difficult to complete the migration.
In addition, just because you can remove the Serializable
attribute, and a program "runs" in one environment does not mean that it will work in another environment. For example, it might work just fine for you on the Visual Studio test web server (which always uses the InProc
session state InProc
) and even on the IIS instance for development, but then maybe the production IIS instance is configured to use a different storage mode.
These differences in environment / configuration are not necessarily limited to ASP.NET applications. There are other applications that can run this or even stand-alone applications (itβs easy to build such a configuration environment).
Finally, you can work with a library that can be used by various applications. Some may need to keep the state serialized, while others may not.
Because of these factors, it is often a very good idea, at least when creating a library, to consider labeling simple value classes or state management classes using [Serializable]
. Keep in mind that this increases the work for testing these classes, and there are restrictions on what can be serialized (i.e. a class containing a socket link or a link to an open file may not be a good candidate for serialization, since open external resources cannot be serialized) so do not abuse it.
You asked if using [Serializable]
would be slower. No, this will not happen. This attribute does not affect performance. However, if the application environment is modified to serialize the object, then yes, performance will be affected. This is a process of serialization and deserialization, which is slower than just storing an object on the heap. [Note that some procedures may be written to search for the Serializable
attribute, and then select serialization, but this is rare; this usually looks like ASP.NET and leaves the administrator or user to decide if they want to change the storage environment.]