views:

228

answers:

7

I used a built in function to create comma separated string using List easily. (It is not split and join but new function) I'm not able to recollect or find it. If some one knows about it and uses it please post a link to that. Framework - .net 2.0

(It is not Join or split - I know about this, .net has new built in function to create CSV format)

Check Jacob G Answer below for what i was looking for let me know your thoughts on it compared to join ;)

And whoever gave me -ve rep need to keep some patience and not hurry

A: 

Check out String.Join.

http://msdn.microsoft.com/en-us/library/57a79xd0.aspx

Ryan Hoffman
A: 

I believe you are looking for the String.Join method?

Otávio Décio
no not join it has comma something in it
rs
A: 
String.Join(",",yourEnumerable.ToArray())
Gregoire
no not join it has comma something in it
rs
A: 

was it string.Join? MSDN

Pharabus
+6  A: 
public static string SomethingElseWithComma(this IEnumerable<string> list)
{
  if(list == null)
      return null;

  return String.Join(",",list.ToArray());
}

ps. don't downvote, just having fun.

Stan R.
best answer by far :)
KP
you forgot "comma" in your method name ;)
empi
fixed ;) ... :P
Stan R.
hey! that must be that method - it sure has comma something in it
empi
i meant word comma :P and it is new namespace Systems.somethingthen it gives a function to create csv strings ,that code is at my home now I'm at work will let you guys know if i find that
rs
I would be interested in your findings
Stan R.
check Jacob G Answer
rs
+3  A: 

Out of the box, I don't think List<T> has any methods or properties which do this. I agree with jsmith it must have been an extension method, etc.

//likely the best you'll do without writing your 
//own extension method or coding SomethingElse.
string.Join(", ", list.ToArray());
KP
+5  A: 

This might be what you're thinking of... You need to reference the System.Configuration dll and import the appropriate namespace.

    List<string> temp = new List<string>();
    temp.Add("a");
    temp.Add("b");
    temp.Add("c");
    CommaDelimitedStringCollection cdsc = new CommaDelimitedStringCollection();
    cdsc.AddRange(temp.ToArray());
    Console.WriteLine(cdsc.ToString());

By the way, I found this class by opening up the documentation and typing the word "comma" in the index.

EDIT
In response to your new question - Assuming that your List is already constructed, String.Join is going to be more performant. This collection just uses a StringBuilder. String.Join has a number of low-level optimization that will make it faster.

(also, not terribly cool to take away the "correct answer" after you change to a new question)

Jacob G
correct this is what i was looking for!
rs
I didn't take away i wanted it to be open till every one sees it , yours is still correct answer (and i mentioned in question) but people tend to not open questions once they are answered. I will select it back after few hours sorry.
rs
I also found out that we can directly create csv string instead of using list to create list first and then comma separated string.
rs