Posted By

dotNetkow on 12/15/10


Tagged

IEnumerable c#


Versions (?)

Create a comma-delimited list from IEnumerable


 / Published in: C#
 

  1. string result = string.Empty;
  2.  
  3. List<int> listOfInts = new List<int>();
  4. foreach (int number in listOfInts)
  5. {
  6. if (number == listOfInts.Last())
  7. {
  8. result += number;
  9. }
  10. else
  11. {
  12. result += string.format("{0},", number);
  13. }
  14. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ronklein on December 29, 2010

I think that using a StringBuilder is more efficient here.

Posted By: vtoman on November 11, 2011

There would be a problem if the last number also appears anywhere else in the list.

You need to login to post a comment.