CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Working with static and dynamic arrays in C#

By Tamara Urry
Posted On Jun 26,2009
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: C#
Print this article.

Working with static and dynamic arrays in C#

 

 

In previous article C# Arrays-Explained, we discussed what an array is, the fact that arrays are 0 based (the first place value is referred to with 0, not 1) and that it can contain values in a collection. Next we need to look at practical application for arrays and how to use them efficiently in code.

Static Arrays

Arrays can be declared in many ways. These first examples demonstrate arrays that are created at “design time” – (programmer sets the value(s) and length). This array is “hard coded”. Hard coded means that the values and length of the array are established at the time the array is declared and isn’t based on user input or stored data. It won’t vary while the program is running. Notice that there is more than one way to declare this type of arrays.

string[] strFruitArray = {"apple", "banana", "orange", "grape", "pineapple"};

string[] strFruitArray = new string[]{"apple", "banana", "orange", "grape", "pineapple"};

string[]strFruitArray;

strFruitArray = new string [5] {"apple", "banana", "orange", "grape", "pineapple"};

Regardless of which method is used to declare the array, referencing the individual values is done the same way

strFruitArray[0] = “apple”

strFruitArray[1] = “banana”

strFruitArray[2] = “orange”

strFruitArray[3] = “grape”

strFruitArray[4] = “pineapple”

 

Dynamic Arrays

Most of the time, we need to have arrays that we won’t know the values or how many items. The next example is an example of a completely dynamic array. The only information set at design time is the data type (int), the variable name (intArray), and that it is an array ([]). The values and the number of values will be based on user input or data retrieved from at runtime. The following is an example of how this type of array is declared.

No length or values set at the time of declaration. Set this at the class level (see full example download) in order for it to be visible to the entire form class.

int[] intArray;

Fixed length at the time of declaration but not the values

intArray = new int[5];

 

Practical Example of a Dynamic Array

In the example below, there is a list box with some values. When the user clicks the “Create Array” button, the array size is set to the number of items in the list box and a for loop is used to add the values to the dynamic array.

private void btnCreateArray_Click(object sender, EventArgs e)

{

//the number of items in the list box is the size of our array.

int intNumItems = lstBoxValues.Items.Count;//get the number of items

strListItems = new string[intNumItems];

 

//use a for iteration to add the items.

//List boxes are also 0 based. The first item in the list will be referred to as lstBoxValues[0].

for (int intX = 0; intX < lstBoxValues.Items.Count; intX++)

strListItems[intX] = lstBoxValues.Items[intX].ToString();

 

//Show array properties in a rich text box named rtbArrayValues

rtbArrayValues.Text = "Total number of elements = " + strListItems.LongLength + "\n" ;//the \n is a return

rtbArrayValues.Text += "The LowerBound() value = " + strListItems.GetLowerBound(0).ToString() + "\n";

rtbArrayValues.Text += "The UpperBound() value = " + strListItems.GetUpperBound(0).ToString() + "\n" ;

//show all of the values using the for iteration

for (int intX = 0; intX <= strListItems.GetUpperBound(0); intX++)

rtbArrayValues.Text += "value " + intX + " is " + strListItems[intX] + "\n";

}

 




To see it all in action, download the sample project


Download Source
Similar Articles