Lets say you have a:
List<string> los = new List<string>();
In this crazy functional world we live in these days which one of these would be best for creating one string by concatenating these:
String.Join(String.Empty, los.ToArray());
StringBuilder builder = new StringBuilder();
los.ForEach(s => builder.Append(s));
string disp = los.Aggregate<string>((a, b) => a + b);
or Plain old StringBuilder foreach
OR is there a better way?
This question is related to
c#
string
performance
functional-programming
My vote is string.Join
No need for lambda evaluations and temporary functions to be created, fewer function calls, less stack pushing and popping.
I would go with option A:
String.Join(String.Empty, los.ToArray());
My reasoning is because the Join method was written for that purpose. In fact if you look at Reflector, you'll see that unsafe code was used to really optimize it. The other two also WORK, but I think the Join function was written for this purpose, and I would guess, the most efficient. I could be wrong though...
As per @Nuri YILMAZ without .ToArray()
, but this is .NET 4+:
String.Join(String.Empty, los);
los.Aggregate((current, next) => current + "," + next);
String.Join() is implemented quite fast, and as you already have a collection of the strings in question, is probably the best choice. Above all, it shouts "I'm joining a list of strings!" Always nice.
string.Concat(los.ToArray());
If you just want to concatenate the strings then use string.Concat() instead of string.Join().
My vote is string.Join
No need for lambda evaluations and temporary functions to be created, fewer function calls, less stack pushing and popping.
string.Concat(los.ToArray());
If you just want to concatenate the strings then use string.Concat() instead of string.Join().
los.Aggregate((current, next) => current + "," + next);
My vote is string.Join
No need for lambda evaluations and temporary functions to be created, fewer function calls, less stack pushing and popping.
I would go with option A:
String.Join(String.Empty, los.ToArray());
My reasoning is because the Join method was written for that purpose. In fact if you look at Reflector, you'll see that unsafe code was used to really optimize it. The other two also WORK, but I think the Join function was written for this purpose, and I would guess, the most efficient. I could be wrong though...
As per @Nuri YILMAZ without .ToArray()
, but this is .NET 4+:
String.Join(String.Empty, los);
If you use .net 4.0 you can use a sorter way:
String.Join<string>(String.Empty, los);
I would go with option A:
String.Join(String.Empty, los.ToArray());
My reasoning is because the Join method was written for that purpose. In fact if you look at Reflector, you'll see that unsafe code was used to really optimize it. The other two also WORK, but I think the Join function was written for this purpose, and I would guess, the most efficient. I could be wrong though...
As per @Nuri YILMAZ without .ToArray()
, but this is .NET 4+:
String.Join(String.Empty, los);
string.Concat(los.ToArray());
If you just want to concatenate the strings then use string.Concat() instead of string.Join().
String.Join() is implemented quite fast, and as you already have a collection of the strings in question, is probably the best choice. Above all, it shouts "I'm joining a list of strings!" Always nice.
string.Concat(los.ToArray());
If you just want to concatenate the strings then use string.Concat() instead of string.Join().
String.Join() is implemented quite fast, and as you already have a collection of the strings in question, is probably the best choice. Above all, it shouts "I'm joining a list of strings!" Always nice.
My vote is string.Join
No need for lambda evaluations and temporary functions to be created, fewer function calls, less stack pushing and popping.
String.Join() is implemented quite fast, and as you already have a collection of the strings in question, is probably the best choice. Above all, it shouts "I'm joining a list of strings!" Always nice.
If you use .net 4.0 you can use a sorter way:
String.Join<string>(String.Empty, los);
Source: Stackoverflow.com