Data Contract vs Serializable | Apps by Alex Beaty

Apps by Alex Beaty

Showcase for my portfolio and service for the Apps that I build and maintain, such as Mancala Fantasy - Online

Data Contract vs Serializable

Tags: Java , .NET , C# ,

I’ve finally started writing the code to get my new project underway. I am learning C# as I go, but finding that it is nearly equivalent to Java. I have completed the basic networking client/server model that I used in my earlier project, Secure Chat Server 1, which uses a two-threads system on the client side and a multiple thread (1 main + X * number of clients) system on the server side. This sets up a chat room-style instant messaging model that echoes a message from a client to all other clients via that client’s corresponding server thread.

In order to send more data than just a text string, I need a way to serialize objects to be sent through my socket streams. In Java, I simply relied on the ObjectOutput stream, a class that implemented Java’s Serializable interface. Little did I know how much it abstracted to make my life easier.

C# has no equivalent to Serializable. The next best thing I have found is something called the Data Contract. It’s not a class or interface, but rather an Attribute, and when you tag your properties with it, you are stating that these properties must be honored in serializing and deserializing your objects.

Here are some of the Microsoft docs I found most useful

Scroll to top tarzipsource code