Thursday, February 16, 2006
« Express Database Editions | Main | Cingular 2125 - Not »

Just came across this little generic gem:

string[] input = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };

int[] output = Array.ConvertAll<string, int>(input, delegate(string s) { return int.Parse(s); });

 

I have a number of web.config files that store lists of integers as comma separated strings. This is a clean way to convert it all to an int array. (It is in need of some exception handling.)

 

<appSettings>

      <add key="MachineIDs" value="44,3,43,566" />

</appSettings>

 

private int[] GetConfigIntArray(string key)

{

    string[] values  = ConfigurationManager.AppSettings[key].Split(',');

    return Array.ConvertAll<string, int>(values, delegate(string s) { return int.Parse(s); });

}

kick it on DotNetKicks.com   Thursday, February 16, 2006 10:46:59 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [5]  |