Reading a file line by line is useful when handling large files without loading everything into memory at once.
✅ Best Practice: Use File.ReadLines() which is more memory efficient.
Example
foreach (string line in File.ReadLines("file.txt")) { Console.WriteLine(line); }
Why use ReadLines()?
Reads one line at a time, reducing overall memory usage. Ideal for large files (e.g., logs, CSVs).
Alternative: Use StreamReader (More Control)
For scenarios where you need custom processing while reading the contents of the file:
using (StreamReader reader = new StreamReader("file.txt")) { string? line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } }
Why use StreamReader?
Lets you handle exceptions, encoding, and buffering. Supports custom processing (e.g., search for a keyword while reading).
When to Use ReadAllLines()? If you need all lines at once, use:
string[] lines = File.ReadAllLines("file.txt");
Caution: Loads the entire file into memory—avoid for large files!
In C#, you can format an integer with commas (thousands separator) using ToString with a format specifier.
int number = 1234567; string formattedNumber = number.ToString("N0"); // "1,234,567" Console.WriteLine(formattedNumber);
Explanation:
"N0": The "N" format specifier stands for Number, and "0" means no decimal places. The output depends on the culture settings, so in regions where , is the decimal separator, you might get 1.234.567.
Alternative:
You can also specify culture explicitly if you need a specific format:
using System.Globalization; int number = 1234567; string formattedNumber = number.ToString("N0", CultureInfo.InvariantCulture); Console.WriteLine(formattedNumber); // "1,234,567"
String interpolation, introduced in C# 6.0, provides a more readable and concise way to format strings compared to traditional concatenation (+) or string.Format(). Instead of manually inserting variables or placeholders, you can use the $ symbol before a string to directly embed expressions inside brackets.
string name = "Walt"; string job = 'Software Engineer'; string message = $"Hello, my name is {name} and I am a {job}"; Console.WriteLine(message);
This would produce the final output of:
Hello, my name is Walt and I am a Software Engineer
String interpolation can also be chained together into a multiline string (@) for even cleaner more concise results:
string name = "Walt"; string html = $@" <div> <h1>Welcome, {name}!</h1> </div>";
Closing a SqlDataReader correctly prevents memory leaks, connection issues, and unclosed resources. Here’s the best way to do it.
Using using statements ensures SqlDataReader and SqlConnection are closed even if an exception occurs.
using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("SELECT * FROM Users", conn)) using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { Console.WriteLine(reader["Username"]); } } // ✅ Auto-closes reader here } // ✅ Auto-closes connection here
This approach auto-closes resources when done and it is cleaner and less error-prone than manual closing.
If you need explicit control, you can manually close it inside a finally block.
SqlDataReader? reader = null; try { using SqlConnection conn = new SqlConnection(connectionString); conn.Open(); using SqlCommand cmd = new SqlCommand("SELECT * FROM Users", conn); reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader["Username"]); } } finally { reader?.Close(); // ✅ Closes reader if it was opened }
This is slightly more error prone if you forget to add a finally block. But might make sense when you need to handle the reader separately from the command or connection.
Register for my free weekly newsletter.