Using C# I want to create one or more text files to store all the data pertaining to a particular 'Thing' on the same line. I am presently leaning towards StringBuilder as per the following.

   , _ThingClass  /* Thing class */ 
   , _ClassType /* Property class type */
   , _Prop0  /* These are generic properties that get manipulated depending on _ThingClass and _ClassType */ 
   , _Prop1        
   , _Prop2  
   , _Prop3   
   , _Prop4

Examples of _ThingClass (Person, Event, Vehicle, Item)
Examples of _ClassType (Teacher, Disaster, Car, Mobile Phone)
Examples of _Prop0-4 (Rachel, Tornado, Fiat, Samsung)

The idea is to break down the string on a line to determine:

  • What kind of Thing is stored on the line (first value), and
  • How to handle the other fields* based on this value. (For example: Adding _Prop4 number value if a 'Heal' _ThingClass or Subtracting if a 'Harm' _ThingClass item, Perhaps even loading from, saving to a different text file so that the files can have only stuff of their own type (maybe easier for sorting).)

While I welcome any snippets of advice relating to this, I'll narrow down the question focus and ask:

Are there any cases where using a StringBuilder to structure data and save to or load from a file would be the way to go?

PS. I have found the content of "Which C# data structure should I use to quantify this information?" to be a possible alternative to StringBuilder - but I guess that there can be more than one valid path.

  • Have you considered CSV or Json? – CodesInChaos Mar 23 '16 at 9:25
  • @CodesInChaos I have considered using commas to separate the values for easier parsing. Json I've not yet heard of - will look into it - thanks! – Avestron Mar 23 '16 at 9:28
  • I have since found this to be Json-relevant. Now I just need to look up serializing/deserializing. stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c – Avestron Mar 23 '16 at 9:54
  • 1
    StringBuilder doesn't store anything, it just builds strings, so I don't understand the question as currently asked. I assume you're trying to ask either if the way you're formatting your arbitrary data is a good idea, or if StringBuilder is a good way to implement serialization of arbitrary data. If so, the answer to both of those is that you're reinventing wheels.such as CSV, JSON, XML, YAML, etc. If the data is "non-arbitrary", then the question you linked is probably the answer. – Ixrec Mar 23 '16 at 9:59
  • @Ixrec I apologize if my question is that unclear (I've contemplated: {_StoreThing = sb.ToString();} ... {file.AddRecord(_StoreThing);} but my fundamentals aren't there. I'm muddling :c) – Avestron Mar 23 '16 at 10:07

Can a StringBuilder be used to do this? Yes.

Can a hammer be used to drive screws into wood. Yes.

Both are possible. Neither are a Good Idea.

What you're describing is a class that takes a line of text and delivers back to you the object represented by that line. That's could be a Factory class, or simply a "Reader" class responsible for loading your file.

Pretty much everything that you put into a StringBuilder has to be dragged back out again (as a String) before you can do anything with it (Replace being the notable exception). So, StringBuilders are good at bolting things together; pulling them apart again (to get to the individual items)? Not so much.

Also remember that a "single line" file format can be easily trashed when storing user-entered text items that can themselves contain line breaks.

  • Thank you kindly. Both answers were really good but you make a very good point about the difficulties of taking strings apart for interpretation. JSON did look interesting but is another tech to add to my learning list for later. As such I have moved on to exploring classes (and nested classes). – Avestron Mar 28 '16 at 13:27

First of all this has nothing to do with StringBuilder or string.Format. The question stays the same no matter how you generate the strings.

You are inventing a new serialization format. This means that you must write serialization and deserialization code.

Normally, this is not a good idea because it costs time and is error-prone. Use something read-made such as JSON, XML (XmlSerializer), BinaryFormatter, protobuf or others.

Which one you pick depends on what requirements you have. Normally, you would pick the one that causes the least amount of labor while fulfilling all requirements. If you don't know what to pick, JSON is a good (easy) start.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.