C# files, JSON, and local data
Read, write, parse, serialize, and protect data at the edges of your program.
Chapter goal: Read, write, parse, serialize, and protect data at the edges of your program.
Simple explanation
A file is long-term storage. Serialization means turning program data into a format such as JSON that can be saved or sent.
In C#, this chapter is about moving data between memory and storage. Start with the idea above. Then connect each symbol to a value or action in the example.
Do not try to remember every symbol. First ask what data the program has, what it does with that data, and what result it creates. Technical words become easier when you connect them to those three questions.
Why this topic is important
Apps need persistence, imports, exports, cached settings, and clean data formats. In C#, the syntax may look different from other languages, but the thinking skill transfers: name the data, choose the right operation, and make the next step obvious.
When to use it
Use file and data handling for config files, save data, logs, reports, and offline-first features.
Example code
var profile = new { name = "Aina", level = 3 };
string text = JsonSerializer.Serialize(profile);
Console.WriteLine(text);
Line-by-line explanation
var profile = new { name = "Aina", level = 3 };— This stores or updatesprofile. The value on the right is worked out first, then saved under that name.string text = JsonSerializer.Serialize(profile);— This stores or updatestext. The value on the right is worked out first, then saved under that name.Console.WriteLine(text);— This is the visible output line. It shows the final value after the earlier work is complete.
What the output means
No guaranteed console output: this example prepares, saves, or converts data.
The output is evidence that the program followed the instructions. If your result is different, read from the first line and write down how each value changes. That is debugging, not failure.
Mistake example
var profile = null; // the code assumes the file and required data always exist
string text = JsonSerializer.Serialize(profile);
Console.WriteLine(text);
This version intentionally shows how the code assumes the file and required data always exist. The changed assignment stores a missing value, or a required line is removed, so later code cannot complete its job safely.
Fixed version
var profile = new { name = "Aina", level = 3 };
string text = JsonSerializer.Serialize(profile);
Console.WriteLine(text);
The corrected version restores the real value or required operation. It fixes the chapter-specific problem: the code assumes the file and required data always exist.
Common mistakes
- Assuming a file exists.
- Parsing JSON without validating required fields.
- Saving secrets in plain text.
Warning: Change one part at a time. If you change many lines together, it becomes harder to learn which change caused the result.
Real use cases
- Load app settings.
- Export reports.
- Cache lessons or API responses.
Practice exercise
- Save one new record to a file.
- Read it back and confirm the value matches what you saved.
- Handle the case where the file does not exist yet.
Tip: If the exercise feels too large, complete only steps 1 to 3. Small working code teaches more than a large unfinished project.
Mini quiz
- Predict the output: what happens if you try to read a file before writing to it?
- Select the safer option: assuming a file exists, or checking before you read it?
- Identify complete vs. partial code: does this snippet handle a missing file?
How to read AI-generated code
Do not copy AI code first. Read it like a detective. Find the data, follow the changes, and locate the final output. Ask AI to explain a line only after you have made your own guess.
- What data goes in?
- What values are stored?
- What calculation or decision happens?
- What is printed, displayed, saved, or returned?
- What can go wrong?
- Can you rename one value and still explain the code?
Language reading check
Find the program entry point, follow function calls one at a time, and keep track of each value's type. Do not jump into a class or helper until you know who calls it.
Before you move on
- I can explain this topic in my own words.
- I can read the small example without AI.
- I can change the example and predict the new result.
- I can find and fix one simple mistake.
- I can name one real project that uses this idea.
Next topic
Next, learn APIs and HTTP. Before opening it, explain this chapter out loud in under one minute.
Open the interactive lesson →