How will you merge strings in C# without using a loop?

 Posted by Niladri.Biswas on 7/11/2012 | Category: C# Interview questions | Views: 1942 | Points: 40
Answer:

Suppose, we have some data in the form of string array as under

new string[] { "Hello,", "How", "Are", "You" }

We need to write a C# program that will concatenate the string and will generate the below output
Hello, How Are You

Solution 1: Using Aggregate function
var input = new string[] { "Hello,", "How", "Are", "You" };

var result = input.Aggregate((a, b) => a + " " + b);
Console.WriteLine(result);


Solution 2: Using Join function
var input = new string[] { "Hello,", "How", "Are", "You" };

var result = string.Join(" ", input);
Console.WriteLine(result);


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response