Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Working With Arrays in c#.

Author: Zunnair

In this article you will learn different issues of the array and it manipulations.

In C#, arrays can be declared as fixed length or dynamic. Fixed length array can stores a predefined number of items, while size of dynamic arrays increases as you add new items to the array. You can declare an array of fixed length or dynamic. You can even change a dynamic array to static after it is defined. For example, the following like declares a dynamic array of integers.

int [] intArray;

The following code declares an array, which can store 5 items starting from index 0 to 4.

int [] intArray;
intArray = new int[5];

The following code declares an array that can store 100 items starting from index 0 to 99.

int [] intArray;
intArray = new int[100];

 

 

Single Dimension Arrays

Arrays can be divided into four categories. These categories are single-dimensional arrays, multidimensional arrays or rectangular arrays, jagged arrays, and mixed arrays.

Single-dimensional arrays are the simplest form of arrays. These types of arrays are used to store number of items of a predefined type. All items in a single dimension array are stored in a row starting from 0 to the size of array –1.

In C# arrays are objects. That means declaring an array doesn’t create an array. After declaring an array, you need to instantiate an array by using the “new” operator.

The following code declares a integer array, which can store 3 items. As you can see from the code, first I declare the array using [] bracket and after that I instantiate the array by calling new operator.

int [] intArray;

intArray = new int[3];

Array declarations in C# are pretty simple. You put array items in curly braces ({}). If an array is not initialized, its items are automatically initialized to the default initial value for the array type if the array is not initialized at the time it is declared.

The following code declares and initializes an array of three items of integer type.

int [] intArray;
intArray = new int[3] {0, 1, 2};

The following code declares and initializes an array of 5 string items.

string[] strArray = new string[5] {"Ronnie", "Jack", "Lori", “Max”, “Tricky”}; 
 
You can even direct assign these values without using the new operator. 
 
string[] strArray = {"Ronnie", "Jack", "Lori", “Max”, “Tricky”}; 
 

You can initialize a dynamic length array as following

string[] strArray = new string[] {"Ronnie", "Jack", "Lori", “Max”, “Tricky”}; 
 

Multi Dimension Arrays

A multidimensional array is an array with more than one dimension. A multi dimension array is declared as following:

string[,] strArray;
 

After declaring an array, you can specify the size of array dimensions if you want a fixed size array or dynamic arrays. For example, the following code two examples create two multi dimension arrays with a matrix of 3x2 and 2x2. The first array can store 6 items and second array can store 4 items respectively.

int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
string[,] names = new string[2, 2] { {"Rosy","Amy"}, {"Peter","Albert"} };
 

If you don’t want to specify the size of arrays, just don’t define a number when you call new operator. For example,

int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
string[,] names = new string[,] { {"Rosy","Amy"}, {"Peter","Albert"} };
 

You can also omit the new operator as we did in single dimension arrays. You can assign these values directly without using the new operator. For example:

int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Rosy", "Amy"}, {"Peter", "Albert"} }; 
 

Jagged Arrays

Jagged arrays are often called array of arrays. An element of a jagged array itself is an array. For example, you can define an array of names of students of a class where a name itself can be an array of three strings – first name, middle name and last name. Another example of jagged arrays is an array of integers containing another array of integers. For example,

int[][] numArray = new int[][] { new int[] {1,3,5}, new int[] {2,4,6,8,10} };
 

Again, you can specify the size when you call the new operator.

Mixed Arrays

Mixed arrays are a combination of multi-dimension arrays and jagged arrays. Multi-dimension arrays are also called as rectangular arrays.

Accessing Arrays using for each Loop

The foreach control statement (loop) of C# is a new to C++ or other developers. This control statement is used to iterate through the elements of a collection such as an array. For example, the following code uses foreach loop to read all items of numArray.

int[] numArray = {1, 3, 5, 7, 9, 11, 13};
foreach (int num in numArray)
{

   System.Console.WriteLine(num.ToString());

}

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working With Strings in c#.

Disclaimer - Privacy
© 2002-2017 DevASP.net