c# - infinite loop stackoverflow list of objects exception -


i have code:

public list<csvuserdata> csvuserlist = new list<csvuserdata>();  public csvuserdata() {     readcsv(@"c:\userdata.csv"); }  public string csvemailedittext { get; set; } public string csvnameedittext { get; set; } public string csvaddressedittext { get; set; } public string csvpostnumedittext { get; set; } public string csvcityedittext { get; set; } public string csvphoneedittext { get; set; } public string csvcommentedittext { get; set; } public string selectpage { get; set; }  private void readcsv(string location) {     var reader = new streamreader(file.openread(location));     string line;     string[] values;     while (!reader.endofstream)     {         line = reader.readline();         values = line.split(',');          csvuserlist.add         (             new csvuserdata             {                 csvemailedittext = values[0],                 csvnameedittext = values[1],                 csvaddressedittext = values[2],                 csvpostnumedittext = values[3],                 csvcityedittext = values[4],                 csvphoneedittext = values[5],             }         );     } } 

i trying read csv file list consists of objects named csvuserdata, class definition displayed above. once class instantiated program getting infinite loop resulting in stackoverflow exception once list memory full, though csv file has 1 row of data. can me , explain why happening?

let’s see:

  1. create new csvuserdata object, call constructor.
  2. readcsv(@"c:\userdata.csv");
  3. inside readcsv: open file, , iterate on lines.
  4. for each line: new csvuserdata { … }
  5. go 1.

so end creating new csvuserdata objects within constructor of csvuserdata type. repeat forever.

you meant make readcsv method static or something, , call once. there no reason why should called constructor. , constructor shouldn’t open file , create stuff based on file; that’s far work constructor.


Comments