C# String Manipulation – Joining List with Commas and ‘and’ for the Last Element

c++list

I know I could figure a way out but I am wondering if there is a more concise solution. There's always String.Join(", ", lList) and lList.Aggregate((a, b) => a + ", " + b); but I want to add an exception for the last one to have ", and " as its joining string. Does Aggregate() have some index value somewhere I can use? Thanks.

Best Answer

You could do this

string finalString = String.Join(", ", myList.ToArray(), 0, myList.Count - 1) + ", and " + myList.LastOrDefault();
Related Question