Tutorial by Examples

string[] strings = new[] {"foo", "bar"}; object[] objects = strings; // implicit conversion from string[] to object[] This conversion is not type-safe. The following code will raise a runtime exception: string[] strings = new[] {"Foo"}; object[] objects = strings;...
int[] arr = new int[] { 0, 10, 20, 30}; // Get Console.WriteLine(arr[2]); // 20 // Set arr[2] = 100; // Get the updated value Console.WriteLine(arr[2]); // 100
An array can be declared and filled with the default value using square bracket ([]) initialization syntax. For example, creating an array of 10 integers: int[] arr = new int[10]; Indices in C# are zero-based. The indices of the array above will be 0-9. For example: int[] arr = new int[3] {7,9,...
int[] arr = new int[] {1, 6, 3, 3, 9}; for (int i = 0; i < arr.Length; i++) { Console.WriteLine(arr[i]); } using foreach: foreach (int element in arr) { Console.WriteLine(element); } using unsafe access with pointers https://msdn.microsoft.com/en-ca/library/y31yhkeb.asp...
Arrays can have more than one dimension. The following example creates a two-dimensional array of ten rows and ten columns: int[,] arr = new int[10, 10]; An array of three dimensions: int[,,] arr = new int[10, 10, 10]; You can also initialize the array upon declaration: int[,] arr = new int...
Jagged arrays are arrays that instead of primitive types, contain arrays (or other collections). It's like an array of arrays - each array element contains another array. They are similar to multidimensional arrays, but have a slight difference - as multidimensional arrays are limited to a fixed nu...
public static class ArrayHelpers { public static bool Contains<T>(this T[] array, T[] candidate) { if (IsEmptyLocate(array, candidate)) return false; if (candidate.Length > array.Length) return false; for (int a = 0; a <...
As we know we can declare an array with default values: int[] arr = new int[10]; This will create an array of 10 integers with each element of the array having value 0 (the default value of type int). To create an array initialized with a non-default value, we can use Enumerable.Repeat from the...
Copying a partial array with the static Array.Copy() method, beginning at index 0 in both, source and destination: var sourceArray = new int[] { 11, 12, 3, 5, 2, 9, 28, 17 }; var destinationArray= new int[3]; Array.Copy(sourceArray, destinationArray, 3); // destinationArray will have 11,12 and...
LINQ provides a method that makes it easy to create a collection filled with sequential numbers. For example, you can declare an array which contains the integers between 1 and 100. The Enumerable.Range method allows us to create sequence of integer numbers from a specified start position and a num...
LINQ provides a built-in function for checking the equality of two IEnumerables, and that function can be used on arrays. The SequenceEqual function will return true if the arrays have the same length and the values in corresponding indices are equal, and false otherwise. int[] arr1 = { 3, 5, 7 };...
All arrays implement the non-generic IList interface (and hence non-generic ICollection and IEnumerable base interfaces). More importantly, one-dimensional arrays implement the IList<> and IReadOnlyList<> generic interfaces (and their base interfaces) for the type of data that they cont...

Page 1 of 1