PY
C#

Python to C#

10 lessons

Progress0%
1Variables & Types2Classes & OOP3Collections & LINQ4Async/Await5Exception Handling6File I/O7Generics8Delegates and Events9Records and Pattern Matching10Interfaces
All Mirror Courses
PY
C#
File I/O
MirrorLesson 6 of 10
Lesson 6

File I/O

Reading and writing files

Introduction

In this lesson, you'll learn about file i/o in C#. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
PY
From Python:

In Python, you're familiar with reading and writing files.

C#
In C#:

C# has its own approach to reading and writing files, which we'll explore step by step.

The C# Way

Let's see how C# handles this concept. Here's a typical example:

C#
C# Example
using System.IO;
using System.Text.Json;

// Read all
string content = File.ReadAllText("input.txt");

// Write
File.WriteAllText("output.txt", "Hello!");

// Line by line
foreach (string line in File.ReadLines("input.txt")) {
    Console.WriteLine(line);
}

// Append
File.AppendAllText("log.txt", "New entry\n");

// Streaming (memory efficient)
using var reader = new StreamReader("input.txt");
while (reader.ReadLine() is string line) {
    Console.WriteLine(line);
}

// JSON
var data = JsonSerializer.Deserialize<MyConfig>(
    File.ReadAllText("config.json"));

Comparing to Python

Here's how you might have written similar code in Python:

PY
Python (What you know)
from pathlib import Path

# Read all
content = Path("input.txt").read_text()

# Write
Path("output.txt").write_text("Hello!")

# Line by line
with open("input.txt") as f:
    for line in f:
        print(line.rstrip())

# Append
with open("log.txt", "a") as f:
    f.write("New entry\n")

# JSON
import json
data = json.loads(Path("config.json").read_text())
Path("out.json").write_text(json.dumps(data, indent=2))
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

C# File.ReadAllText = Python Path.read_text()

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

Python 'with open' → C# 'using var reader = new StreamReader'

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

C# foreach over File.ReadLines = Python 'for line in f'

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

C#
In C#:

C# System.Text.Json = Python's json module

Step-by-Step Breakdown

1. Static File Class

C#'s File class provides static methods that mirror Python's pathlib one-liners for common operations.

PY
Python
Path("file.txt").read_text()
C#
C#
File.ReadAllText("file.txt")

2. Streaming

For large files, use StreamReader with 'using' for automatic cleanup — like Python's 'with open()' pattern.

PY
Python
with open("file.txt") as f:
    for line in f: ...
C#
C#
using var f = new StreamReader("file.txt");
while (f.ReadLine() is string line) { ... }

3. JSON

C#'s System.Text.Json deserializes into typed objects — safer than Python's dict-based json.loads.

PY
Python
data = json.loads(text)  # dict
C#
C#
var data = JsonSerializer.Deserialize<Config>(text); // typed

Common Mistakes

When coming from Python, developers often make these mistakes:

  • C# File.ReadAllText = Python Path.read_text()
  • Python 'with open' → C# 'using var reader = new StreamReader'
  • C# foreach over File.ReadLines = Python 'for line in f'
Common Pitfall
Don't assume C# works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • File.ReadAllText/WriteAllText = Path.read_text/write_text
  • using StreamReader = with open()
  • File.ReadLines for line-by-line iteration
  • System.Text.Json for typed JSON deserialization
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in C# to practice these concepts.
PreviousNext