SIGN UP MEMBER LOGIN:    
ARTICLE

Dynamic and Static Array Declarations

Posted by Robert Pattinson Articles | C# Language June 06, 2009
Arrays can be declared in many ways. These examples demonstrate arrays that are created at “design time” (static) and at "run time" (dynamic)
Reader Level:

There are 2 methods commonly used for declaring arrays.  While both have their place and usage, most programmers prefer the dynamic array that is declared at run-time to provide the most flexibility.  The static array is great if you have a set structure that will never change.  For instance, an array that holds the days of the week, months in the year, or even the colors in a rainbow.  These won't change and they won't be data driven so in this case, a static array will be best.

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";
}
The full source code can be found at www.jettechnical.com

Login to add your contents and source code to this article
share this article :
post comment
 

Before writing the looping, check if your source collection have a CopyTo or ToArray method which can be useful in copying items to an Array.  Eg, the above code could avoid the loop by using

   lstBoxValue.Items.CopyTo(strListItems, 0).

Also, Collections like List<T> contains a method T[] ToArray() which returns an Array with all items in the list.

In addition, there are many methods in System.Array class which are very useful.

-Pramod B. R
   

Posted by Pramod Bhakta Jun 17, 2009
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor