NOTICE:
AFTER THE CURRENT CONTEST THIS TUTORIAL WILL BE COMPLETELY REDONE.
Today I'm going to show you how to use arrays in C#.
OK so to get started we need to know, what is an array? Well the best way I can explain it is: An array is a variable that can hold multiple values without the need to assign multiple variables. So for instance we could turn these three variables: 'number1', 'number2', 'number3' into one variable(an array) 'numbers' and just access the value we want from it.
To use an array we declare it like this:
int[] Numbers;Then assign how many values it will hold like this:
Numbers = new int[9];Now we have an array that will hold nine values, we can set the values like this:
Numbers[0] = 1; Numbers[1] = 2; Numbers[2] = 3; Numbers[3] = 4; Numbers[4] = 5; Numbers[5] = 6; Numbers[6] = 7; Numbers[7] = 8; Numbers[8] = 9;Important note: an array starts at 0 not 1 so be sure to remember that because if you assigning your array values like this:
Numbers[[COLOR=red]1[/COLOR]] = 1; Numbers[[COLOR=red]2[/COLOR]] = 2; Numbers[[COLOR=red]3[/COLOR]] = 3; Numbers[[COLOR=red]4[/COLOR]] = 4; Numbers[[COLOR=red]5[/COLOR]] = 5; Numbers[[COLOR=red]6[/COLOR]] = 6; Numbers[[COLOR=red]7[/COLOR]] = 7; Numbers[[COLOR=red]8[/COLOR]] = 8; Numbers[[COLOR=red]9[/COLOR]] = 9;Your going to get an error.
We can also make an array like this:
int[] Numbers = new int[6]{9,1,3,7,8,4};So what that did was make a new int array named Numbers with "[6]" values and assigned the values in one line of code here "{9,1,3,7,8,4}" but for now we will use the first way as it is easy to understand.
OK so if your wandering how to make a string or double array we can make it the same as the int array just change the names.
[COLOR=red]string[/COLOR][] lowletter; lowletter = new [COLOR=red]string[/COLOR][26]; lowletter[0] = [COLOR=red]"[/COLOR]a[COLOR=red]"[/COLOR];// don't forget the [ " ] at the beginning and end of you string. lowletter[1] = "b"; lowletter[2] = "c"; lowletter[3] = "d"; lowletter[4] = "e"; lowletter[5] = "f"; lowletter[6] = "g"; lowletter[7] = "h"; lowletter[8] = "i"; lowletter[9] = "j"; lowletter[10] = "k"; lowletter[11] = "l"; lowletter[12] = "m"; lowletter[13] = "n"; lowletter[14] = "o"; lowletter[15] = "p"; lowletter[16] = "q"; lowletter[17] = "r"; lowletter[18] = "s"; lowletter[19] = "t"; lowletter[20] = "u"; lowletter[21] = "v"; lowletter[22] = "w"; lowletter[23] = "x"; lowletter[24] = "y"; lowletter[25] = "z";For a double we just change it to:
[COLOR=red]double[/COLOR][] Numbers; Numbers = new [COLOR=red]double[/COLOR][6]; Numbers[0] = 1.9; Numbers[1] = 1.7; Numbers[2] = 1.2; Numbers[3] = 3.3; Numbers[4] = 3.7; Numbers[5] = 7.9;OK now that we now how to declare an array lets print it out in a text box.
So if you haven't already, create a new windows form app from File->New Project->Windows Form Application, and drag a button and text box to the form.
OK so now double click the button and put in this code:
string[] lowcase; lowcase = new string[10]; lowcase[0] = "a"; lowcase[1] = "b"; lowcase[2] = "c"; lowcase[3] = "d"; lowcase[4] = "e"; lowcase[5] = "f"; lowcase[6] = "g"; lowcase[7] = "h"; lowcase[8] = "i"; lowcase[9] = "j"; textBox1.Text += lowcase[0]; textBox1.Text += lowcase[1]; textBox1.Text += lowcase[2]; textBox1.Text += lowcase[3]; textBox1.Text += lowcase[4]; textBox1.Text += lowcase[5]; textBox1.Text += lowcase[6]; textBox1.Text += lowcase[7]; textBox1.Text += lowcase[8]; textBox1.Text += lowcase[9];Now this will print out the values of the array, but you probably don't want to say " textBox1.Text += lowcase[0];" over and over again so we can replace it with a loop like this:
for (int i = 0; i != (lowcase.Length); i++) { textBox1.Text += (lowcase[i]); }Which runs a loop and prints the array values until "i" is equal to "lowcase.Length" which is how many values there are in the array which in this case would be 10.
OK so now you know how to declare an array set its values and print it in a loop, I hope you understood every thing clearly if you have any questions/problems tell me.
As always +rep and comments welcome.

Edited by CommittedC0der, 12 August 2011 - 11:38 AM.
Added A Little More Info.