Introduction to Arrays in C#



What is an array?

In simplest terms, an array is an organized collection. It can be a collection of anything. The important thing to remember is that it is a collection.

We use arrays every day

A pack of M&Ms is a great example of an array. It's easy for us to imagine a bag of this popular candy. We can imagine each one of the M&Ms as they are taken from the bag. They are all basically the same but different colors. Even though they are different colors, they are all chocolate candy with a hard candy A bag of M&M's is an array of datatype M&M's. It is a collection of candy.

How do I declare an array?

Declaring an array is very much like declaring other variables. The only difference is that you are declaring a variable that will hold more than one object. Let's compare these two declarations.

String strEmotion = "happy";
String[] strEmotions = {"happy","sad","elated","afraid","angry","peaceful"};


Both of the variables above, strEmotion and strEmotions, have been declared using Explicit Declaration which means the value is set at the same time they are created.

By using the brackets [] , the compiler knows that the variable strEmotions will hold more than one value. The number of items and the values is set with = {"happy","sad","elated","afraid","angry","peaceful"}; In this case, we have set the size of the array to 6 because we have added 6 values.

Now we take a closer look at these 2 variable declarations.

Variable Name Datatype Number of Value(s) Value(s)
strEmotion String 1 happy
strEmotions String 6 happy, sad, elated, afraid, angry, peaceful


How do I get a specific value?

Getting the value of each item isn't hard once you understand how the array refers to each item. At first it might be a little confusing because arrays use a zero based reference. This means that the first item in the list is in the 0 position. Huh? Okay, look at the next lines and you'll understand.

String [] strEmotions = {"happy","sad","elated","afraid","angry","peaceful"};

strEmotions[0] = happy Value in the 0 positon of the array
strEmotions[1] = sad Value in the 1 position of the array
strEmotions[2] = elated Value in the 2 position of the array
strEmotions[3] = afraid Value in the 3 position of the array
strEmotions[4] = angry Value in the 4 position of the array
strEmotions[5] = peaceful    Value in the 5 position of the array


Putting it all to work
 


In this example, we have an array of data type System.Drawing.Colors that we will use to change the background color of a form. This very simple application will help in understanding how to reference each of the different items in an array. It also shows how to find out which control called the function.

Let's look at each lineā€¦

First an array of data type System.Drawing.Color is declared and four colors, Gree, Red, Yellow, and Blue are added at the same time. Notice the brackets [] which signify an array declaration. Because we populate the array at the time we declare it, we say that it was explicitly declared.

System.Drawing.Color[] myColorArray = {System.Drawing.Color.Green, System.Drawing.Color.Red , System.Drawing.Color.Yellow, System.Drawing.Color.Blue};

Now we add a method and assign it to the click event of all the radio buttons. We could have written this under four different methods, one for each of the radio buttons. But since they are almost identical, it's better to add them in one method and then find out which radio button called it. This also makes it much easier to support later on down the road (One Method rather than Four Methods).


private void ChangeColors(object sender, EventArgs e)
{
/*Get a reference to the radio button that called the function. We know that the control that is going to call this method will be a Radio Button so first we create a variable of datatype Radio Button. Then we set our variable = to the sender object. But we need first to convert the sender variable to a datatype of Radio*/
System.Windows.Forms.RadioButton rbtn = (System.Windows.Forms.RadioButton)sender;

Next find out which radio button it was by finding the name of the button.

if (rbtn.Name == "rbtnGreen")
   this.BackColor = myColorArray[0];// <---first item is the array is in the 0 position
if (rbtn.Name == "rbtnRed")
   this.BackColor = myColorArray[1];// <---second item is the array is in the 1st position
if (rbtn.Name == "rbtnYellow")
   this.BackColor = myColorArray[2];// <---third item is the array is in the 2nd position
if (rbtn.Name == "rbtnBlue")
   this.BackColor = myColorArray[3];// <---fourth item is the array is in the 3rd position
}

For full source code and other tutorials, visit jettechnical.com


Similar Articles