Wednesday, 22 March 2017

Arrays in C#

The Array Type


Another data type is the Array, which can be thought of as a container that has a list of storage locations for a specified type. When declaring an Array, specify the type, name, dimensions, and size.
An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type stored at contiguous memory locations.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Declaring Arrays

To declare an array in C#, you can use the following syntax:
datatype[] arrayName;
where,
· datatype is used to specify the type of elements in the array.
· [ ] specifies the rank of the array. The rank specifies the size of the array.
· arrayName specifies the name of the array.
For example,
double[] balance;

Initializing an Array

Declaring an array does not initialize the array in the memory. When the array variable is initialized, you can assign values to the array.
Array is a reference type, so you need to use the new keyword to create an instance of the array. For example,
double[] balance = new double[10];

Assigning Values to an Array

You can assign values to individual array elements, by using the index number, like:
double[] balance = new double[10];
balance[0] = 4500.0;
You can assign values to the array at the time of declaration, as shown:
double[] balance = { 2340.0, 4523.69, 3421.0};
You can also create and initialize an array, as shown:
int [] marks = new int[5]  { 99,  98, 92, 97, 95};
You may also omit the size of the array, as shown:
int [] marks = new int[]  { 99,  98, 92, 97, 95};
You can copy an array variable into another target array variable. In such case, both the target and source point to the same memory location:
int [] marks = new int[]  { 99,  98, 92, 97, 95};
int[] score = marks;
When you create an array, C# compiler implicitly initializes each array element to a default value depending on the array type. For example, for an int array all elements are initialized to 0.

Accessing Array Elements

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example,
double salary = balance[9];

ArrayList
It represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. However, unlike array you can add and remove items from a list at a specified position using an index and the array resizes itself automatically. It also allows dynamic memory allocation, adding, searching and sorting items in the list.

Methods and Properties of ArrayList Class

The following table lists some of the commonly used properties of the ArrayList class:
Property
Description
Capacity
Gets or sets the number of elements that the ArrayList can contain.
Count
Gets the number of elements actually contained in the ArrayList.
IsFixedSize
Gets a value indicating whether the ArrayList has a fixed size.
IsReadOnly
Gets a value indicating whether the ArrayList is read-only.
Item
Gets or sets the element at the specified index.
The following table lists some of the commonly used methods of the ArrayList class:
Sr.No.
Methods
1
public virtual int Add(object value); 
Adds an object to the end of the ArrayList.
2
public virtual void AddRange(ICollection c); 
Adds the elements of an ICollection to the end of the ArrayList.
3
public virtual void Clear(); 
Removes all elements from the ArrayList.
4
public virtual bool Contains(object item); 
Determines whether an element is in the ArrayList.
5
public virtual ArrayList GetRange(int index, int count); 
Returns an ArrayList which represents a subset of the elements in the source ArrayList.
6
public virtual int IndexOf(object); 
Returns the zero-based index of the first occurrence of a value in the ArrayList or in a portion of it.
7
public virtual void Insert(int index, object value); 
Inserts an element into the ArrayList at the specified index.
8
public virtual void InsertRange(int index, ICollection c); 
Inserts the elements of a collection into the ArrayList at the specified index.
9
public virtual void Remove(object obj); 
Removes the first occurrence of a specific object from the ArrayList.
10
public virtual void RemoveAt(int index); 
Removes the element at the specified index of the ArrayList.
11
public virtual void RemoveRange(int index, int count); 
Removes a range of elements from the ArrayList.
12
public virtual void Reverse(); 
Reverses the order of the elements in the ArrayList.
13
public virtual void SetRange(int index, ICollection c); 
Copies the elements of a collection over a range of elements in the ArrayList.
14
public virtual void Sort(); 
Sorts the elements in the ArrayList.
15
public virtual void TrimToSize(); 
Sets the capacity to the actual number of elements in the ArrayList.

No comments:

Post a Comment