An array is a collection of similar datatype elements with same name.
In any variable we can store on value at one time.
If we have to store more than one values (collection of values) with same data type, array will be useful.
If we have to store more than one values (collection of values) with same data type, array will be useful.
Syntax:
---------
datatype[] variablename=new datatype[size];
Example:
-----------
int[] arr=new int[3];
Here [3] is the size of the array that allows 3 values to add into the array.
Add elements into the array
---------------------------------
There are different methods to add values into the array
while declaring array we can add values as below
int[] arr=new int[]{1,2,3};
We can add below, this could be the useful way to runtime adding.
arr[0]=1;
arr[1]=5;
arr[2]=3;
Print the elements in the array
--------------------------------------
//Print the elements in the array
for (int i = 0; i <arr.Length; i++)
{
Console.WriteLine("Element value id : " + arr[i]);
}
Properties of an array
--------------------------
Sort
------
Array.Sort(arr);
Sort sets the elements in an array in ascending order.
//Print the elements of an array in ascending order
for (int i = 0; i <arr.Length; i++)
{
Console.WriteLine("Element value id : " + arr[i]);
}
Reverse
----------
Array.Reverse(arr);
Reverse sets the elements in an array in reverse order.
This could be useful to print the elements in descending order.
First, we need to sort and then reverse.
//Print the elements of an array in ascending order
for (int i = 0; i <arr.Length; i++)
{
Console.WriteLine("Element value id : " + arr[i]);
}
Copy
--------
The copy function is to copy one array elements into another array
int[] arrayb = new int[arr.Length];
Array.Copy(arr, arrayb , arr.Length);
Count
-------
Count gives the number of elements exists in an array.
arr.Count();
Limitations of an array
----------------------------
1) Fixed size/memory
2)Homogeneous type of data
3)Doesn't support to insert elements at required locations
4)Doesn't support to delete elements from array directly.
To overcome the above limitations we have a concept in .Netframework call "Collections" which we are going to discuss on future soon.




SQL Server
C#.Net
ASP.Net
ADO.Net
jQuery
No comments:
Post a Comment