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