Skip to Content

IPAddress class serializable?

Why do I get the error that System.Net.IPAddress cannot be serialized because it does not have a default public constructor when the IPAddress class is marked serializable?

Posted on One min read

Back in 2002 I posted a question in Usenet about the IPAddress class being serializable, but not serializing in ASP.NET. I just found the post, and noticed that no one responded with the reason why:

There are two types of serialization in .NET: System.Runtime.Serialization and System.Xml.Serialization. Runtime serialization uses the SerializableAttribute while XML serialization does not. The attribute is irrelevant for XML serialization, which is used by ASP.NET.

The error I received was:

System.Net.IPAddress cannot be serialized because it does not have a default public constructor

One of the requirements of the XmlSerializer is that the type being serialized must have a default public constructor. So the answer is:

Yes, it’s serializable using runtime serialization, but not XML serialization.

2006 Update

With .NET 3.0, DataContracts are used by newer serialization engines and are now preferred over the SerializableAttribute.