Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Below's an example of reading a file of the format you specify and pushing the results into a List (or ArrayList if you prefer) of People. Based on this you should be able to create a list of strings if that's your preferences, though I'd suspect you'd want a list of people? </p> <pre><code>class Program { static void Main(string[] args) { string fn = @"c:\myfile.txt"; IList list = new ArrayList(); FileReader(fn, ref list); for (int i = 0; i &lt; list.Count; i++) { Console.WriteLine(list[i].ToString()); } Console.ReadKey(); } public static void FileReader(string filename, ref IList result) { using (StreamReader sr = new StreamReader(filename)) { string firstName; string lastName; string marks; IgnoreHeaderRows(sr); while (!sr.EndOfStream) { firstName = sr.EndOfStream ? string.Empty : sr.ReadLine(); lastName = sr.EndOfStream ? string.Empty : sr.ReadLine(); marks = sr.EndOfStream ? string.Empty : sr.ReadLine(); result.Add(new Person(firstName, lastName, marks)); } } } const int HeaderRows = 2; public void IgnoreHeaderRows(StreamReader sr) { for(int i = 0; i&lt;HeaderRows; i++) { if(!sr.EndOfStream) sr.ReadLine(); } } } public class Person { string firstName; string lastName; int marks; public Person(string firstName, string lastName, string marks) { this.firstName = firstName; this.lastName = lastName; if (!int.TryParse(marks, out this.marks)) { throw new InvalidCastException(string.Format("Value '{0}' provided for marks is not convertible to type int.", marks)); } } public override string ToString() { return string.Format("{0} {1}: {2}", this.firstName, this.lastName, this.marks); } public override int GetHashCode() { return this.ToString().GetHashCode(); } } </code></pre>
    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.
 

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