c# - Many else and if statements? -


i reading csv file has column names in first line , values in line >1. need position of column name. way can think of either switch or ifs. read somewhere in case , faster (better) ifs. file has many columns (~120). wondering if there alternative(s).

private static void get_position(string line, performance p) {     string[] line_split = line.split(',');     (int = 0; < line_split.length; i++)     {        if (line_split[i].contains(@"(0)\% processor time"))         {             p.percore[0] = i;         }         else if (line_split[i].contains(@"(1)\% processor time"))         {             p.percore[1] = i;         }         else if (line_split[i].contains("private bytes"))         {}         else if (line_split[i].contains("dpc")         {          }          //on , on , on else ifs 

what preventing using loop?

for (int = 0; < line_split.length; i++) {    for(var j = 0; j < 120; j++)    {       if(line_split[i].contains(@"(" + j + ")\% processor time"))       {          p.percore[j] = i;       }    } ... 

to maintain same functionality if else if use break inside conditional.

edit: edit made clear there no clear pattern string in contains. still, if writing out 120 if/else if statements should store looking in type of collection. example, list work. access index j of collection in loop:

... var listofsearchitems = new list<string>() { "private bytes", "dpc" };  (int = 0; < line_split.length; i++) {    for(var j = 0; j < 120; j++)    {       if(line_split[i].contains(listofsearchitems[j])       {          p.percore[j] = i;       }    } ... 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -