Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would change it to the following:</p> <pre><code>static void Main(string[] args) { // StreamReader is IDisposable which should be wrapped in a using statement using (StreamReader reader = new StreamReader(@"d:\TEST.txt")) { while (!reader.EndOfStream) { string line = reader.ReadLine(); // make sure we have something to work with if (String.IsNullOrEmpty(line)) continue; string[] cols = line.Split(','); // make sure we have the minimum number of columns to process if (cols.Length &lt; 4) continue; double a = Convert.ToDouble(cols[1]); Console.Write(a); int b = Convert.ToInt32(cols[3]); Console.WriteLine(b); Console.WriteLine(); } } } </code></pre> <p>Some notes here: </p> <ol> <li>StreamReader implements IDisposable, so you should wrap it in a using clause so that it is properly disposed of.</li> <li>Don't name things like "sLine". That form of Hungarian is commonly recognized as seriously bad practice. Even Microsoft says don't do it.</li> <li>You're dealing with columns, not rows. So that variable should be named appropriately.</li> <li>Always test to make sure you have all of the columns you need before blindly accessing them.</li> <li>Normally, I wouldn't use Convert.ToDouble or Convert.ToInt32. It's much safer to use TryParse to make sure it was able to convert. The code you have will blow if cols[1] and cols[3] had non-numeric data.</li> <li>You can use the @ symbol in front of a string to tell the compiler that it doesn't need to be escaped.</li> <li>It's much cleaner to simply "continue" a loop instead of wrapping it in a if statement.</li> <li>Setting a String variable to a blank string then immediately setting it to some other value causes the blank to stay in memory for the entire scope. In other words, it's wasting memory. Granted, in this case it's a micro-optimization, but it never hurts to use best practices all of the time.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload