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

// setup initial vars
var myList = new List<bool>();
for (int x = 0; x < 10000000; x++)
    myList.Add(false);  

var containsAllFalse = false;
Stopwatch sw = new Stopwatch();

// start test
sw.Start();
containsAllFalse = !myList.Any(x => x);
sw.Stop();

// get result for Any
var timeAny = sw.ElapsedMilliseconds;

// reset variable state (just in case it affects anything)
containsAllFalse = false;   

// start test 2
sw.Restart();
containsAllFalse = myList.All(x => x == false);
sw.Stop();

// get result for All
var timeAll = sw.ElapsedMilliseconds;

// reset variable state (just in case it affects anything)
containsAllFalse = false;   

// start test 3
sw.Restart();
containsAllFalse = !myList.Exists(x => x == true);
sw.Stop();

// get result for All
var timeExists = sw.ElapsedMilliseconds;

// reset variable state (just in case it affects anything)
containsAllFalse = false;   

// start test 4
sw.Restart();   
containsAllFalse = !myList.Contains(true);          
sw.Stop();

// get result from Contains
var timeContains = sw.ElapsedMilliseconds;

// print results
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);
        }

    }