❔ 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
Pobiega9mo ago
There is a Concept called "serialization" that will help you do this
Buddy
Buddy9mo 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
Pobiega9mo 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
Buddy9mo 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
Pobiega9mo ago
Right, so manually making your own binary writer/reader
Buddy
Buddy9mo ago
Pretty much yep
Pobiega
Pobiega9mo ago
Sure
Buddy
Buddy9mo ago
Since it's just strings and not custom classes, serializing and deserializing is easy. No need for an external library or anything.
Pobiega
Pobiega9mo ago
Yep, that's fine 🙂
Buddy
Buddy9mo 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
Accord9mo 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
More Posts
✅ C# null exception with SQLI was trying to deploy my application to another computer to try it, and I got an error where it cou❔ Problem with getting GPU infoI'm currently working on a console program that shows some information of user's PC for fun. I have ✔ Simple code structure questionLet's say I have interface "IShapes" in file Shapes.cs Now, I'd like to create 3 more classes. Circl❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFII have the following: ```cs public sealed unsafe partial class Test : IDisposable { public ❔ How do I hide or show a div based on the value of a property of my selected option?When I select a habit type, that habit type has a Measurability property, and I want to show or hide❔ going from prefix commands to slash commands in discord.netSo the creator of this repo has archived the repo. Does anyone know, how can i easily convert to us❔ CodeTaskFactory problemLook at the image, ill try other projects later Also i've repair the vs install but nothing❔ How to convert a hex string to a Span<byte>?Is there an allocation free method to convert a hex string into the corresponding bytes without allo❔ Where can I find the implementation of System.Object.ReferenceEquals?I'd like to know how the reference comparison works under the hood but I've never looked up the sour✅ How can I make my code reusable?Hi! I asked a question before to calculate the average of 5 text boxes in one textbox. I wrote a co