Collection



  • The Collection object provides a convenient way to refer to a related group of items as a single object
  • The items, or elements, in a collection need only be related by the fact that they exist in the collection
  • Elements of a collection do not have to share the same data type
  • Closely related data can be handled more efficiently when grouped together into a collection
  • Instead of writing separate code to handle each individual object, you can use the same code to process all the elements of a collection
  • To manage a collection, use the Array class and the System.Collections classes to add, remove, and modify either individual elements of the collection or a range of elements. An entire collection can even be copied to another collection
  • A collection is similar to an Array but provides additional flexibilities
Differences between Arrays and Collections

SNO
         ARRAYS
                COLLECTIONS
01
We cannot insert a new element in to the Array
We can Insert a New Element into the collection anywhere, then following elements will flush down automatically
02
We cannot delete any element from the Array
We can delete any element from the Collection, then following elements will flush up automatically
03
Array size is Fixed
Collection size is not Fixed as long as Memory is available we can add the Elements
04
We get either Insufficient Memory or Out of Memory problem with Array
We do not get either Insufficient Memory or Out of Memory problem with collection
05
Array Supports only Single Data Type
Collection can store all Data type values

 Methods with Collection

1.     Add(Object Item)
2.     Insert(int Index, object Item)
3.     Remove(object Item)
4.     RemoveAt(int Index)
5.     Clear( )
Properties with Collection


    Count
 
1)Add(object Item)

This method is used to add a new Element in to the collection


2)Insert(int Index,object Item)
  • Used to Add a new element into the collection at given Index.
  • When new element is added the following elements will flush down automatically.

3)Remove(object Item)
  • Used to delete the Element from the collection using Element value .
  • When the Element is Deletd from the Collection then the following Elements will flush up Automatically
                              

4)RemoveAt(int Index)
  • This Method is used to Delete An Element from the Collection using Index value.
  • When the Element is Deletd from the Collection then the following Elements will flush up Automatically



5)Clear( )

This Method is used to Delete all the Elements from the collection

1)Count      
              
This Property will return count of the Elements present in the collection

S.Count         :-         4
 

No comments:

Post a Comment