❔ How can I make it so an array saves changes when I turn off the program

Basically I'm making an array of strings that can be removed or added to - how can I make it so if say I add the string "Dog" to it, when I come back to the code it will still have "Dog" in it without me having to re-add it. I'm in college so I have very little knowledge of C# but I am trying to learn. I use Visual Studio Community. Thanks
12 Replies
Pobiega
Pobiega15mo ago
There is a Concept called "serialization" that will help you do this
Buddy
Buddy15mo ago
You need to store it to disk, IE permanent storage. You can do the serialization any way you'd like, I personally prefer to store it as binary.
Pobiega
Pobiega15mo ago
You could serialize your array/list to a string, then write that string to a file Your program can then try and read the file when it starts and deserialize the content to an array/list again I'm curious, what would be your suggested method for saving a string list as binary?
Buddy
Buddy15mo ago
Depends, in an insecure way by this.
----------------------------------------
Magic Bytes
Version
----------------------------------------
Type (byte) | Length (4 bytes) | Data
----------------------------------------
Magic Bytes
Version
----------------------------------------
Type (byte) | Length (4 bytes) | Data
Pobiega
Pobiega15mo ago
Right, so manually making your own binary writer/reader
Buddy
Buddy15mo ago
Pretty much yep
Pobiega
Pobiega15mo ago
Sure
Buddy
Buddy15mo ago
Since it's just strings and not custom classes, serializing and deserializing is easy. No need for an external library or anything.
Pobiega
Pobiega15mo ago
Yep, that's fine 🙂
Buddy
Buddy15mo ago
The added data is just to know how much data to retrieve, it's type, and version is for compatibility, while the magic bytes is to check if the file is valid and not just a file with renamed extension
DisfunctionalCucumber
Thank you both I'll look into it now
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server