f:http://www.dotnetperls.com/array
Array. A prism refracts light. As white light enters, it splits apart. It becomes an array of colors. Consider a string array—we can use strings for these colors.
Arrays are inside many things. An array has an element type. Its elements are accessed with an index. An array cannot be resized—it can only be replaced.
String arrays. We begin with string arrays. Square brackets are used for all arrays. The syntax is fairly simple, but we need to memorize it.
Note:All of these constructs are equivalent in the compiled code. Please choose the one you think is clearest.
Var:The var type is an implicitly-typed local variable. It is the same to the compiler as an explicit string array.
Tip:The fourth array allows us to test each value or insert logic as we assign it. This is sometimes useful.
Based on: .NET 4.6 C# program that initializes string arrays class Program { static void Main() { // String arrays with 3 elements: string[] arr1 = new string[] { "one", "two", "three" }; string[] arr2 = { "one", "two", "three" }; var arr3 = new string[] { "one", "two", "three" }; string[] arr4 = new string[3]; arr4[0] = "one"; arr4[1] = "two"; arr4[2] = "three"; } }
Tip:We can think of an array as a class with a variable number of fields, each accessed with an index.
C# program that receives array parameter using System; class Program { static void Main() { // Three-element array. int[] array = { -5, -6, -7 }; // Pass array to Method. Console.WriteLine(Method(array)); } /// <summary> /// Receive array parameter. /// </summary> static int Method(int[] array) { return array[0] * 2; } } Output -10
C# program that returns array reference using System; class Program { static void Main() { // Write array from Method. Console.WriteLine(string.Join(" ", Method())); } /// <summary> /// Return an array. /// </summary> static string[] Method() { string[] array = new string[2]; array[0] = "THANK"; array[1] = "YOU"; return array; } } Output THANK YOU
C# program that gets first array element using System; class Program { static void Main() { int[] array = new int[2]; // Create an array. array[0] = 10; array[1] = 20; Test(array); Test(null); // No output. Test(new int[0]); // No output. } static void Test(int[] array) { if (array != null && array.Length > 0) { int first = array[0]; Console.WriteLine(first); } } } Output 10
C# program that gets last array element using System; class Program { static void Main() { string[] arr = new string[] { "cat", "dog", "panther", "tiger" }; // Get the last string element. Console.WriteLine(arr[arr.Length - 1]); } } Output tiger
Foreach:With this loop, no indexes are needed. The loop itself handles the indexes. This makes some code simpler.
C# program that uses for each, for-loops on array using System; class Program { static void Main() { string[] array = new string[2]; array[0] = "Socrates"; array[1] = "Plato"; for (int i = 0; i < array.Length; i++) { // Get element by index. string element = array[i]; Console.WriteLine(element); } Console.WriteLine(); // Write blank line. foreach (string element in array) { // Cannot assign element in this loop. Console.WriteLine(element); } } } Output Socrates Plato Socrates Plato
Warning:IndexOf methods return -1 when no element is found. This value often must be checked in an if-statement.
C# that uses Array.IndexOf using System; class Program { static void Main() { string[] array = { "cat", "dog", "bird", "fish" }; // The dog string is at index 1. int dogIndex = Array.IndexOf(array, "dog"); Console.WriteLine(dogIndex); // There is no monkey string in the array. // ... So IndexOf returns -1. int monkeyIndex = Array.IndexOf(array, "monkey"); Console.WriteLine(monkeyIndex); } } Output 1 -1
Elements:The second part of the Test class is a property accessor. It provides a clean way for external code to access the internal array.
Indexer:The final part of the Test class is called an Indexer. An indexer uses the this-keyword.
Class IndexerNote:The indexer shown receives one parameter, an integer, and returns a value based on it.
C# that uses string array in class class Program { static void Main() { // Create new instance with string array. Test test = new Test(); // Loop over elements with property. foreach (string element in test.Elements) { System.Console.WriteLine(element); } // Get first string element. System.Console.WriteLine(test[0]); } } public class Test { /// <summary> /// String array field instance. /// </summary> string[] _elements = { "one", "two", "three" }; /// <summary> /// String array property getter. /// </summary> public string[] Elements { get { return _elements; } } /// <summary> /// String array indexer. /// </summary> public string this[int index] { get { return _elements[index]; } } } Output one two three one
Join:This example uses the Join method to combine the three string literals within the "elements" array.
JoinSplit:Finally we invoke Split to change our joined string back into a string array. The two string arrays are separate in memory.
SplitC# that uses Join, Split using System; class Program { static void Main() { string[] elements = { "cat", "dog", "fish" }; Console.WriteLine(elements[0]); // ... Join strings into a single string. string joined = string.Join("|", elements); Console.WriteLine(joined); // ... Separate joined strings with Split. string[] separated = joined.Split('|'); Console.WriteLine(separated[0]); } } Output cat cat|dog|fish cat
Start:Try creating a shortcut in Windows to your C# executable. The args array is empty when no arguments are passed.
Here:I added the argument string "hello world" to the command in the Windows shortcut. The two strings are received into the args array.
C# that uses args string array using System; class Program { static void Main(string[] args) { // ... Loop over arguments passed to this program. foreach(string value in args) { Console.WriteLine("Argument: {0}", value); } Console.ReadLine(); } } Output Argument: hello Argument: world
An array is a fixed collection of same-type data that are stored contiguously and that are accessible by an index.
Algorithms in C++ Third Edition: google.comArrays are the simplest and most common type of structured data.
Code Complete: google.comA review. An array is a fixed region of memory that contains elements. Arrays store things like string references. We find them at the core of important types.