Using statement with file streams
📖 Scenario: You want to read the contents of a text file safely and correctly in C#. Using the using statement helps you open the file, read it, and automatically close it when done.
🎯 Goal: Build a simple program that reads all lines from a file called example.txt using a using statement and prints the content to the console.
📋 What You'll Learn
Create a string variable
filePath with the value "example.txt".Create a
using statement to open a StreamReader for filePath.Inside the
using block, read all lines from the file using ReadToEnd().Print the file content to the console.
💡 Why This Matters
🌍 Real World
Reading files safely is common in many applications like reading configuration, logs, or user data.
💼 Career
Understanding file streams and resource management is essential for software developers working with file input/output.
Progress0 / 4 steps