Arrays serve as a powerful tool for organizing and storing groups of data elements of the same data type as a cohesive unit. They provide a convenient means of accessing individual elements within the array through their numeric indices. It is important to note that array indices commence at zero, meaning the first element in the array is accessed using an index of zero, the second element with an index of one, and so forth.

Initializing an array

When initializing an array, the default values of numeric elements are automatically set to zero. This ensures that numeric arrays are prepopulated with a consistent starting value, facilitating subsequent operations and calculations. On the other hand, reference elements within an array are set to null by default. This is particularly useful when working with arrays that store object references, allowing for clear identification of uninitialized or empty elements within the array.

Dim arr(6) As String arr(0) = "Sunday" arr(1) = "Monday"
string[] arr = new string[7]; arr[0] = "Sunday"; arr[1] = "Monday";

The abovecode declare a string array of 7 strings and assign some values to it.

With arrays as the tool, developers are provided with the functionality to work on data using a structured pattern, thereby helping them to perform tasks in an organized manner. Arrays can be utilized for storing an unbundled list of strings, series of integer values, or collection of objects; regardless of actual data involved, arrays deliver a congruent and simplified mechanism for manipulating related data. The zero-based index system and default values along with the automatic career make this tool very usable and effective as arrays as the fundamental data relocation and retrieval mechanism.

We can access the Arrays elements by providing its numerical index.

Dim str As String = arr(1)
string str = arr[1];

Above statement returns the second value from the Array.

The following ASP.NET program fill an arra with seven days in a week and bind it to a ListBox control.

<html xmlns=""> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <br /> <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox><br /> </div> </form> </body> </html>
Full Source | C#
using System; using System.Collections; public partial class _Default : System.Web.UI.Page { protected void Button1_Click(object sender, EventArgs e) { string[] arr = new string[7]; arr[0] = "Sunday"; arr[1] = "Monday"; arr[2] = "Tuesday"; arr[3] = "Wednsday"; arr[4] = "Thursday"; arr[5] = "friday"; arr[6] = "Saturday"; ListBox1.DataSource = arr; ListBox1.DataBind(); } }
Full Source | VB.NET
Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim arr(6) As String arr(0) = "Sunday" arr(1) = "Monday" arr(2) = "Tuesday" arr(3) = "Wednesday" arr(4) = "Thursday" arr(5) = "Friday" arr(6) = "Saturday" ListBox1.DataSource = arr ListBox1.DataBind() End Sub End Class