Ashish Agrawal Ashish Agrawal - 4 months ago 10
C# Question

Fasted way to load fields from comma separated string

I have a string like: "ABC, 23, SEE" and correspondingly I have a class named Employee which has properties like Name, Age and Designation.

Now, the string in the example above contains the values in order of Name, Age and Designation. What would be the fastest way to create an instance of the Employee class considering that I have an array of such strings with total number of 100 elements.


I wouldn't bother finding the 'fastest' way. You should find 'fast enough' way. And for 100 strings virtually any method will work fast enough for practical usage. The simplest (the fastest to implement, if we define requirement that way...) would be:

var parts = dataStr.Split(new []{','});
var employee = new Employee(parts[0].Trim(), parts[1].Trim(), parts[2].Trim());