The variables you have been working with so far have only been able to hold one value at a time. Your integer variables can only hold one number, and your strings one chunk of text. An array is a way to hold more than one variable at a time. Think of a lottery programme. If you're just using single variables, you'd have to set up your lottery numbers like this:
If your array needs to hold floating point numbers, you'd set your array up like this:
The next thing you need to do is to tell C# how big your array will be. The size of an array is how many items it is going to hold. You do it like this:
So the two lines would be:
int[ ] lottery_numbers;
lottery_numbers = new int[49];
If you prefer, you can put all that on one line:
To assign a value to an array, you use the square brackets again. Here's the syntax:
Another way to assign values in array is by using curly brackets. If you only have a few values going in to the array, you could set it up like this:
lottery_number_1 = 1;
lottery_number_2 = 2;
lottery_number_3 = 3;
lottery_number_4 = 4;
etc
Instead of doing that, an array allows you to use just one identifying name that refers to lots of numbers. lottery_number_2 = 2;
lottery_number_3 = 3;
lottery_number_4 = 4;
etc
How to set up an Array
You set up an array like this:int[ ] lottery_numbers;
So you start with the type of array you need. In the line above, we're telling C# that the array will hold numbers (int). After the array type, you need a pair of square brackets. There should be no space between the array type and the first bracket. After the square brackets then type a space, followed by the name you want to use for your array, lotter_numbers in our case.If your array needs to hold floating point numbers, you'd set your array up like this:
float[ ] my_float_values;
An array that needs to hold text would be set up like this:string[ ] my_strings
So it's pretty much just like setting up a normal variable, except you type a pair of square brackets after int, or float, or string.The next thing you need to do is to tell C# how big your array will be. The size of an array is how many items it is going to hold. You do it like this:
lottery_numbers = new int[49];
So the name of your array goes before an equals sign ( = ). After the equals sign, you type the word new. This tells C# that it is a new object. After a space, you need the array type again (int for us). Next comes some square brackets. This time, however, you type the size of the array between the brackets. In the code above, we're saying that the array will hold 49 numbers.So the two lines would be:
int[ ] lottery_numbers;
lottery_numbers = new int[49];
If you prefer, you can put all that on one line:
int[ ] lottery_numbers = new int[49];
But you're doing two things at once, here: before the equals sign, you're telling C# that you want to set up an array; after the equals sign, you're creating a new array object of a particular size.Assigning values to your arrays
So far, you have just set up the array, and created an array object. But the array doesn't yet hold any values. (Well it does, because C# assigns some default values for you. In the case of int arrays, this will be just zeros. But they're not your values!)To assign a value to an array, you use the square brackets again. Here's the syntax:
array_name[ position_in_array ] = array_value;
So you start with the name of your array, followed by a pair of square brackets. In between the square brackets, you need a position in your array. You then type an equals sign, and the value that is going in that position. Here's an example using our lottery numbers:lottery_numbers[0] = 1;
lottery_numbers[1] = 2;
lottery_numbers[2] = 3;
lottery_numbers[3] = 4;
First of all, note that the first position in a C# array is zero, and not 1. This is slightly confusing, and can trip you up! But we're telling C# to assign a value of 1 to the first position in the array, a value of 2 in the second position, a value of three in the third, and so on. Just bear in mind that array positions start at 0.lottery_numbers[1] = 2;
lottery_numbers[2] = 3;
lottery_numbers[3] = 4;
Another way to assign values in array is by using curly brackets. If you only have a few values going in to the array, you could set it up like this:
int[] lottery_numbers = new int[4] { 1, 2, 3, 4 };
So we've set up the array the same way as before - all on one line. This time, we have a pair of curly brackets at the end. In between the curly brackets, type the values for your array, and separate each value with a comma.
No comments:
Post a Comment