In this post we will discuss about array and arraylist in C#.net.
Also you can check out:
- Constraints in SQL Server 2008
- Encrypt and Decrypt a file in C#.Net
- Export gridview data to excel sheet in Asp.net
Array:
- Arrays allow you to store a series of values that have the same data type.
- The values can be accessed by using its index number. And the index starts with 0 and the highest index is one less than the number of elements.
- Below is a string array:
string[] stringArray = new string[5];
Here new keyword is used to initialize the array.
Also you can directly put the values like below:
string[] stringArray = {"1", "2", "3", "4","5"};
- We can access the values like below:
string value = stringArray[3];
The value will be 4;
- If you create an array, you can not resize the array.
- If you declared a interger array then only interger can be stored, if you try to initialize any other thing then it will give exception.
- Similarly you can create multidimentional array like below:
int[,] intArray = {{1, 2}, {3, 4}, {5, 6}, {7, 8},{9,10}};
ArrayList:
- ArratList supports any type of object and always allows dynamic resizing, means the size can be increased or decreased.
- Syntax to create array:
ArrayList arr = new ArrayList();
Put the value like below:
arr.Add("Value1");
arr.Add("Value2");
arr.Add("Value3");
And we can retrieve the value like below:
string value = Convert.ToString(arr[1]);
Also you can check out:
- Constraints in SQL Server 2008
- Encrypt and Decrypt a file in C#.Net
- Export gridview data to excel sheet in Asp.net
Array:
- Arrays allow you to store a series of values that have the same data type.
- The values can be accessed by using its index number. And the index starts with 0 and the highest index is one less than the number of elements.
- Below is a string array:
string[] stringArray = new string[5];
Here new keyword is used to initialize the array.
Also you can directly put the values like below:
string[] stringArray = {"1", "2", "3", "4","5"};
- We can access the values like below:
string value = stringArray[3];
The value will be 4;
- If you create an array, you can not resize the array.
- If you declared a interger array then only interger can be stored, if you try to initialize any other thing then it will give exception.
- Similarly you can create multidimentional array like below:
int[,] intArray = {{1, 2}, {3, 4}, {5, 6}, {7, 8},{9,10}};
ArrayList:
- ArratList supports any type of object and always allows dynamic resizing, means the size can be increased or decreased.
- Syntax to create array:
ArrayList arr = new ArrayList();
Put the value like below:
arr.Add("Value1");
arr.Add("Value2");
arr.Add("Value3");
And we can retrieve the value like below:
string value = Convert.ToString(arr[1]);