c# combine array into string code example
Example 1: c# list join
List<string> names = new List<string>() { "John", "Anna", "Monica" };
var result = String.Join(", ", names.ToArray());
Example 2: c# combine list of bool
var myList = new List<bool>();
for (int x = 0; x < 10000000; x++)
myList.Add(false);
var containsAllFalse = false;
Stopwatch sw = new Stopwatch();
sw.Start();
containsAllFalse = !myList.Any(x => x);
sw.Stop();
var timeAny = sw.ElapsedMilliseconds;
containsAllFalse = false;
sw.Restart();
containsAllFalse = myList.All(x => x == false);
sw.Stop();
var timeAll = sw.ElapsedMilliseconds;
containsAllFalse = false;
sw.Restart();
containsAllFalse = !myList.Exists(x => x == true);
sw.Stop();
var timeExists = sw.ElapsedMilliseconds;
containsAllFalse = false;
sw.Restart();
containsAllFalse = !myList.Contains(true);
sw.Stop();
var timeContains = sw.ElapsedMilliseconds;
var percentFaster = Math.Round((double)timeAny / timeContains, 2);
Console.WriteLine("Elapsed via Any = {0}ms", timeAny);
Console.WriteLine("Elapsed via All = {0}ms", timeAll);
Console.WriteLine("Elapsed via Exists = {0}ms", timeExists);
Console.WriteLine("Elapsed via Contains = {0}ms", timeContains);
Console.WriteLine("Contains is ~{0}x faster than Any!", percentFaster);
Example 3: c# array.join
using System;
public class Demo {
public static void Main(string[] args) {
string[] strArr = {"AB", "BC", "CD", "DE", "EF", "FG", "GH", "IJ" };
Console.WriteLine("String Array...");
foreach(string s in strArr) {
Console.WriteLine(s);
}
string str = string.Join("*", strArr);
Console.WriteLine("Result (after joining) = " + str);
}
}
Example 4: combine two arraylist c#
class Program
{
static void Main(string[] args)
{
ArrayList CountryList1 = new ArrayList();
ArrayList CountryList2 = new ArrayList();
CountryList1.Add("Pakistan");
CountryList1.Add("Nepal");
CountryList2.Add("Butan");
CountryList2.Add("Srilanka");
CountryList1.AddRange(CountryList2);
}
}