Today I had to read values from multiple CVS-files. I wanted to build a testable and reusable solution instead of the ugly sequential spaghetti that easily happens.

So here is a sample of a line:

var lineReadFromCsv =

To increase the readability I created enumerations that point to the index of the position of the values in each line that I wanted to read:

public enum DataFormatField
    Id = 0,
    GivenName = 1,
    Surname = 2,
    DateOfBirth = 3,
    UniqueId = 4,

I created a class called ParserParameter that contains enums that

public class ParserParameter
    prop Enum FirstName { get; set; }

Last but not least an extension class the provides a generic method TryGetValueOrDefault(). It takes an enum member as parameter. It can convert int, long, float, decimal, double, guid, datetime as well as string and can easily be extended.

int id;
if (fields.TryGetValueOrDefault(
    out id))