3D-kreativ
3D-kreativ

Reputation: 9301

Save objects to file

I'm building a small application and I need to store my data to file. I read some about saving objects. I have done this before in Java, but that was some years ago. Then I saved the hole array of objets and I wonder if I can do the same in C# and how that is done?

I'm using a List that is called Animals and I have also serialized that class, because that was a demand in this task. If I have serialized the Animal class, then I guess I don't need to serialize sub classes to Animal?

Some help is preciated, thanks!

EDIT:

Hi agian! I have made some test code to save a list and I just wonder if I'm doing right? I have not added any Try/Catch code yet. When I should open the file and read the list, will the list be loaded as a whole? Feedback is preciated!

filestream = new FileStream(filePath, FileMode.Create);
        BinaryFormatter b = new BinaryFormatter();
        b.Serialize(filestream, animals);
        filestream.Close();

Upvotes: 0

Views: 1487

Answers (3)

Alberto De Caro
Alberto De Caro

Reputation: 5213

Serialization of objects is a great thing. It let you store the state of an object, to share the object with other system, to pass objects between the business layer anjd the front end of an application.

In general, always state what kind of serialization you need: binary, xml, json? The way the two system communicate each other affects the choise.

You say you don't need xml serialization. Don't you? It is very uneasy to manage logging, debugging and tracking of binary serialized objects. You don't need XML? It's ok, let's use a custom serializer then, but help yourself avoiding binary serialization. Unless it is required!!

Anyway, here the msdn links:

Upvotes: 1

JonnyRaa
JonnyRaa

Reputation: 8038

It looks like you're doing a quick example, in which case using serialisable is probably fine, however you'll quickly discover it's a real pain to use and any change to the classes marked as serialisable will render old files unreadable.

This makes serialisation suitable for quite transient storage eg - persisting the state of a batch program inbetween steps or transferring data across a network. If you are actually writing files, you're probably better off designing a file format.

A nice language to use is xml - which is basically designed for outputting tree like structures and it is easy to add to as everything is read by name and not position.

If you just have quite simple data you could just design a text format; some simple options are to use tab or space seperation. If you have polymorphic types you may need to think about this a little bit to get data structures which work for them all, or you can have all sub type fields and then distinguish them with a 'type' entry eg:

(space seperated)
Type NumberOfLegs NumberOfFins
Fish - 2
Badger 4 -
SeaDog 3.5 7

Upvotes: 0

Manuel Amstutz
Manuel Amstutz

Reputation: 1388

IF your class consists only of Serializable types you can mark your base class AND all subclasses as serializable. This is done with the Serializable attribute

Upvotes: 0

Related Questions