Using Statement for Resource Cleanup
📖 Scenario: Imagine you are writing a program that reads text from a file. To keep your program clean and safe, you want to make sure the file is properly closed after reading, even if something goes wrong.
🎯 Goal: You will create a program that reads all lines from a file using the using statement to automatically clean up the file resource.
📋 What You'll Learn
Create a string variable with the file path
Create a
StreamReader object inside a using statementRead all lines from the file inside the
using blockPrint the lines to the console
💡 Why This Matters
🌍 Real World
Reading files safely is common in many programs, like loading settings or processing data files.
💼 Career
Understanding resource cleanup with <code>using</code> is important for writing reliable and maintainable C# applications.
Progress0 / 4 steps