Property: array.Length

Initialization

From Enumerable
// array with same default value
var array = Enumerable.Repeat(String.Empty, n).ToArray();

// from any IEnumerable
var array = ienumerable.ToArray();
Single dimention
var array = new string[n]; 
var array = new int[] { 1, 2 };
int[] array = { 1, 2 };
Multi dimentional
var arr = new int[n, m];
var arr = new int[,] { {1, 2, 3}, {4, 5} };
int[,] arr = { {1, 2, 3}, {4, 5} };
Jagged array - array of arrays
int[][] jaggedArray = new int[n][]; // 2nd diention is variable, can't be specified
var jaggedArray = new int[][]{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};
 

Array Methods

Array.Sort(arr);
Array.Reverse(arr);
Array.Resize(ref arr, n);
Array.Find(arr, Predicate<T>);
Array.Exists<T>(arr, Predicate<T>)
Array.Empty<T>() // get an empty array
Array.ForEach<T>(arr, Action<T>)

Array method details

Conversion to string

array to string

var array = str.Split(string[] separator, StringSplitOptions options)

// separator is null or "" => deault (white-space characters) will be used
// StringSplitOptions.None >> returned array contains both empty and non-empty items
// StringSplitOptions.StringSplitOptions.RemoveEmptyEntries >> returned array contains only non-empty items

string to array

var array = String.Join(string separator, params string[] value);
var array = String.Join(string separator, IEnumerable<T> values);
// there are other overloads

Conversion to list

array to list

var list = new List<T>(array);
var list = array.ToList();

var list = new List<T>();
list.AddRange(array);

list to array

string[] arr = list.ToArray();

Conversion to dictionary

array to dictionary

var dict = array.ToDictionary(key => key, value => value); // key == value == each item

// to avoid duplicate key, use index as key
var dictionary = array.Select((v, i) => new {Key = i, Value = v}).ToDictionary(o => o.Key, o => o.Value);

// to avoid duplicate key, use hashset
var hashset = new HashSet<string>(array);
var dict = hashset.ToDictionary(key => key, value => value);

dictionary to array

var array = dict.Values.ToArray();
var array = dict.Keys.ToArray();

T[] array = new T[dict.Count];
dict.Values.CopyTo(array, 0); // starting from 0 index

// using LINQ - has overhead (calling Lambda each time)
var array = dict.Select(pair => pair.Value).ToArray();

N.B: CopyTo() is faster than ToArray()