Saturday 7 May 2011

Concepts of Arrays in C#

Array:
     An array is the collection of similar type of objects. Array in C# is different for the array of C++ and other languages because they are
objects. This provides them useful methods and property. Arrays allow a group of elements of a particular type to be stored
in a contiguous block of memory. Array types derive from System.Array and are declared in C# using brackets ([]).

Syntax- datatype [] array-name;
      e.g.- int [] age;
    The square brackets ([]) tell the C# compiler that you are declaring an array, and the type specifies the type of the elements it will
contain. In the previous example, age is an array of integers. Instantiate an array using the new keyword. For example:
age = new int[5]; This declaration sets aside memory for an array holding five integers.

Multidimensional Array:
           
Multidimensional Arrays of two types  Rectangular Array and Jagged Array Rectangular Array represents n-dimensional blocks.
      e.g.- int [ , ,] age = new int[17,20,34];  
           Jagged Arrays are arrays of arrays.


int [][][] books = new int [3][][];
                    for (int i = 0; i < 3; i++)
                        {
                     books[i] = new int [4][];
                          for (int j = 0; j < 4; j++)
                     books[i][j] = new int [5];
                          }
// assign an element
                books1 [1,1,1] = books [1][1][1] = 7;
Indexer: Indexers are usually known as smart array in C#.It is used for treating  object as an array.Defining an indexer in C# is much like same as
defining properties. Or we can say that an indexer is a member that enables an object to be indexed in the same way as an array.
  Syntax-
<access modifier> <return type> this [argument list]
{
get
{
// Write here some code for Get
}
set
{
// Write here some code for Get
}
}
 

Collections:  Collections are the enumerable data structure in C# that can be assessed using indexes or keys. Types of collections in C# are given below-
    System.Collections namespace

 This provides a lot of classes, methods and properties to interact with the varying data structures that are supported by it.  The interfaces
that are defined in this namespace include:


  •       IEnumerable

  •       IEnumerator

  •       ICollection

  •       IList

  •       IDictionary
       System.Collections.Stack
       System.Collections.Queue  Both  are derived from ICollection Interface.
    The collections that inherit the IDictionary interface include:
             System.Collections.SortedList     System.Collections.Hashtable
    The IList interface represents collections that only have value.  The following are the classes that extend this interface.
           System.Array

  •    System.Collections.ArrayList

  •    System.Collections.Specialized.StringCollection
      Concrete Collection Classes:
    ArrayList class- This works by maintaining an internal array of objects that is replaced with a larger array when it reaches its capacity of elements.
    BitArray class-  It is a dynamically sized array of Boolean values. It is more memory-efficient  than a simple array of bools because it uses only one bit for each value.
    Hashtable class- A Hashtable is a standard dictionary (key/value) data structure that uses a hashing  algorithm to store and index values efficiently.
    Queue class- A Queue is a standard first-in, first-out (FIFO) data structure, providing simple operations to enqueue, dequeue, peek, etc.
    SortedList class-A SortedList is a standard dictionary data structure that uses a binary-chop search to index efficiently.
    Stack class- A Stack is a standard last-in first-out (LIFO) data structure.
    StringCollection class- A StringCollection is a standard collection data structure for storing strings.
     

  • No comments:

    Post a Comment