-1

how to return list values without using foreach or for?

For example i have these variables :

List<string> mylist = new List<string>();
mylist.Add("First");
mylist.Add("Second");
mylist.Add("Third");
string finalstring = "";

Instead of using this code :

foreach(string str in mylist)
{
    finalstring = str + " - ";
}

I want to use something like this :

finalstring = mylist.something.somthing...;

And get an output like this :

"First - Second - Third"

Is it possible ?

3
  • 1
    Have you done any reasearch on your own before? If you did, it's hard to believe you missed String.Join. Commented Jan 16, 2018 at 18:24
  • @Sefe , I haven't done researches much, i make my own methods most the times Commented Jan 16, 2018 at 18:29
  • @Ali - I recommend always research, especially before asking for help from others - it's the difference between learning and becoming better to not.. Commented Jan 16, 2018 at 18:31

1 Answer 1

0

Just use string.Join;

List<string> mylist = new List<string>();
mylist.Add("First");
mylist.Add("Second");
mylist.Add("Third");
string finalstring = string.Join("-",mylist);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.