C# – How to parse a comma-separated string from app.config

On one project, I had to parse comma-separated values from app.config and use them to make decisions in the code. To make lookups efficient, I put the parsed values into a HashSet. I’ll show how to do this.

In this example, I’ll load comma-separated integers from a setting in app.config called “retryStatusCodes”:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> </startup> <appSettings> <add key="retryStatusCodes" value="408,429,503"/> </appSettings> </configuration>
Code language: HTML, XML (xml)

Note: These integers are HTTP status codes. In my project, we used these to determine when to retry failed HTTP requests.

First, load the string from app.config with ConfigurationManager. Second, split and parse it into integers with a Linq one-liner. Third, load it into a HashSet. The following code does this:

using System.Configuration; using System.Linq; //Step 1 - Read from app.config var csv = ConfigurationManager.AppSettings["retryStatusCodes"]; //Step 2 - Split it and parse to the target type var errorCodes = csv.Split(',').Select(i => Int32.Parse(i)); //Step 3 - Store it in the proper data structure var retryStatusCodes = new HashSet<int>(errorCodes); Console.WriteLine($"We have {retryStatusCodes.Count} retry status codes"); Console.WriteLine($"Retry when status code is 404? {retryStatusCodes.Contains(404)}"); Console.WriteLine($"Retry when status code is 429? {retryStatusCodes.Contains(429)}");
Code language: C# (cs)

Note: You have to add a reference to System.Configuration in your project.

This outputs the following:

We have 3 retry status codes Retry when status code is 404? False Retry when status code is 429? True
Code language: plaintext (plaintext)

This example shows how to parse comma-separated integers, but you can apply this approach to any target type.

Avoiding exceptions

Notice that the code above has no error handling. If there’s invalid data, such as if a value can’t be parsed to an integer, then it’ll simply throw an exception. This may be desirable, depending on your requirements. In general, when you have invalid config data (and no good defaults), then it’s better to let your program crash.

If you want to avoid exceptions and simply ignore bad values, you can loop through the split string and use Int32.TryParse() (instead of Int32.Parse()), like this:

HashSet<int> ParseIntoLookup(string csv) { var lookup = new HashSet<int>(); if (string.IsNullOrEmpty(csv)) { return lookup; } foreach (var value in csv.Split(',')) { int tmp; if (Int32.TryParse(value, out tmp)) { lookup.Add(tmp); } } return lookup; }
Code language: C# (cs)

Here’s an example of using this on CSV strings with a mix of valid and invalid data:

var allValid = ParseIntoLookup("408,429,503"); Console.WriteLine($"Valid Count: {allValid.Count}"); var trailingComma = ParseIntoLookup("408,"); Console.WriteLine($"Valid Count: {trailingComma.Count}"); var nonInts = ParseIntoLookup("408,abc,503"); Console.WriteLine($"Valid Count: {nonInts.Count}");
Code language: C# (cs)

This outputs the following, showing that doesn’t throw an exception and ignores invalid values:

Valid Count: 3 Valid Count: 1 Valid Count: 2
Code language: plaintext (plaintext)

Leave a Comment