Difference between revisions of "Arrays"
(→Arrays in C#) |
|||
Line 9: | Line 9: | ||
==Arrays in C#== | ==Arrays in C#== | ||
<p>There are several ways of declaring arrays in C#. There are several ways shown below:</p> | <p>There are several ways of declaring arrays in C#. There are several ways shown below:</p> | ||
− | + | ||
+ | Creates an <u>empty</u> array that will return integers. | ||
<syntaxhighlight lang="csharp" line>int[] arrayname;</syntaxhighlight> | <syntaxhighlight lang="csharp" line>int[] arrayname;</syntaxhighlight> | ||
− | + | ||
+ | Creates an <u>empty</u> array that will return integers, but it's size is 10. (10 people in a queue). | ||
<syntaxhighlight lang="csharp" line>arraynumbers = new int[10];</syntaxhighlight> | <syntaxhighlight lang="csharp" line>arraynumbers = new int[10];</syntaxhighlight> | ||
− | + | ||
+ | Creates an array with values for each number in the queue. | ||
<syntaxhighlight lang="csharp" line>int[] numbers = {1, 2, 3, 4, 5};</syntaxhighlight> | <syntaxhighlight lang="csharp" line>int[] numbers = {1, 2, 3, 4, 5};</syntaxhighlight> | ||
− | + | ||
+ | Don't forget that it can be any data type. | ||
<syntaxhighlight lang="csharp" line>string[] strings = {"one", "two", "three", "four", "five"};</syntaxhighlight> | <syntaxhighlight lang="csharp" line>string[] strings = {"one", "two", "three", "four", "five"};</syntaxhighlight> | ||
+ | |||
<p>To get something from an array, you can simply do the same as I explained before.</p> | <p>To get something from an array, you can simply do the same as I explained before.</p> | ||
<syntaxhighlight lang="csharp" line> | <syntaxhighlight lang="csharp" line> |
Revision as of 15:28, 15 December 2016
Understanding arrays
Arrays are like a queue of people, but person number 0 is the first person in the queue. Imagine it like this:
As you can see from the image above, you know that if you wanted the 3rd person in the queue you would select them something like "queue person 2" and if you wanted the last person in the queue you would say something like "queue person 4" and if you wanted the first person in the queue, you would say "queue person 0". Essentially, instead of starting counting from 1, you start counting from 0.
You should also remember that it is possible to create an array within an array. Think of it as a queue, and in each queue is another queue.
Arrays in C#
There are several ways of declaring arrays in C#. There are several ways shown below:
Creates an empty array that will return integers.
1 int[] arrayname;
Creates an empty array that will return integers, but it's size is 10. (10 people in a queue).
1 arraynumbers = new int[10];
Creates an array with values for each number in the queue.
1 int[] numbers = {1, 2, 3, 4, 5};
Don't forget that it can be any data type.
1 string[] strings = {"one", "two", "three", "four", "five"};
To get something from an array, you can simply do the same as I explained before.
1 string[] strings = {"one", "two", "three", "four", "five"}; //Declares strings as an array of strings.
2 Console.WriteLine(strings[0]); //Writes the first thing in the array
3 Console.WriteLine(strings[2]); //Writes the third thing in the array
4 Console.WriteLine(strings[4]); //Writes the fifth thing in the array
5 //Output:
6 //one
7 //three
8 //five