0

I want to join two different sentences in one sentence:

 var list1 = new List<string>();
  list1.Add("Hello ");
  list1.Add("my name is John");

when I print elements of this list:

foreach (var item in list1)
           {
               Console.WriteLine(item);
           }

Output:

Hello 

my name is John

What I want to get is:

Hello my name is John

kn1ghtxx
  • 41
  • 6
  • Does this answer your question? [Concat all strings inside a List using LINQ](https://stackoverflow.com/questions/559415/concat-all-strings-inside-a-liststring-using-linq) – Drag and Drop Apr 08 '21 at 08:37
  • And https://stackoverflow.com/questions/3575029/c-sharp-liststring-to-string-with-delimiter – Drag and Drop Apr 08 '21 at 08:38

1 Answers1

0

Yes, the name of the method is Join:

string joined = string.Join(" ", list1);

You don't need that trailing space after "Hello ".

However, in this case you could also have used Console.Write instead of Console.WriteLine.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939