SlideShare a Scribd company logo
1 of 14
1
ArrayList in C#
Collections
Collection classes are specialized classes for data storage and retrieval. These classes
provide support for stacks, queues, lists, and hash tables. Most collection classes
implement the same interfaces.
Collection classes serve various purposes, such as allocating memory dynamically to
elements and accessing a list of items on the basis of an index etc. These classes create
collections of objects of the Object class, which is the base class for all data types in C#.
Various Collection Classes and Their Usage
The following are the some commonly used classes of the System.Collection
namespace.
Collections
Class Description and Usage
ArrayList
It represents 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.
Hashtable
It uses a key to access the elements in the collection.
A hash table is used when you need to access elements by using key, and you can identify a
useful key value. Each item in the hash table has a key/value pair. The key is used to access
the items in the collection.
SortedList
It uses a key as well as an index to access the items in a list.
A sorted list is a combination of an array and a hash table. It contains a list of items that can
be accessed using a key or an index. If you access items using an index, it is an ArrayList, and
if you access items using a key , it is a Hashtable.
The collection of items is always sorted by the key value.
Stack
It represents a last-in, first out collection of object.
It is used when you need a last-in, first-out access of items. When you add an item in the list,
it is called pushing the item and when you remove it, it is called popping the item.
Queue
It represents a first-in, first out collection of object.
It is used when you need a first-in, first-out access of items. When you add an item in the
list, it is called enqueue and when you remove an item, it is called deque.
BitArray
It represents an array of the binary representation using the values 1 and 0.
It is used when you need to store the bits but do not know the number of bits in advance. You
can access items from the BitArray collection by using an integer index, which starts from
zero.
ArrayList
 The ArrayList collection class is a conventional arrays and
provides dynamic resizing of the collection.
Method / Property Description
Add Adds an object to the end of the ArrayList.
Capacity Property that gets and sets the number of elements for which space
is currently reserved in the ArrayList.
Clear Removes all elements from the ArrayList.
Contains Determines whether an element is in the ArrayList.
Count Read-only property that gets the number of elements stored in the
ArrayList.
IndexOf Returns the zero-based index of the first occurrence of a value in the
ArrayList
Insert Inserts an element into the ArrayList at the specified index.
Remove Removes the first occurrence of a specific object from the ArrayList.
RemoveAt Removes the element at the specified index of the ArrayList.
TrimToSize Sets the capacity to the actual number of elements in the ArrayList.
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.
Methods of the ArrayList class:
Sr. No. Methods
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.
Example
The following example demonstrates the concept:
You have been requested to develop an application which maintain set of items and prices
during run time:
Sharaf DG needs a program to store Items and its Prices. They want to have the options to
sort by items’ name as well to sort prices.
Design an interface similar to the following
Example : Interface Design
Tools names are labeled as shown in below figure.
txtItem txtPrice
btnAdd
btnDP
btnON
btnAPbtnOP
btnAN
Example : Coding
Validate entered price: is double
Add Names & Prices to the ArrayLits
Convert to double data typeConvert to double data type
Use collection library
Example : Coding
Validate entered price: is double
Add Names & Prices to the ArrayLits
Convert to double data typeConvert to double data type
Example : Coding
Displays items names
1. The original list of names
2. The ascending sorted names
3. The descending sorted names
Example : Coding
Displays items Prices
1. The original list of Prices
2. The ascending sorted Prices
3. The descending sorted Prices
Example : Runtime
Enter these values with the same sequence
TV 1500
Flash disk 120
Camera 680
Mobile 550
Apple Laptop 2200
Example : Runtime
Enter these values with the same sequence
TV 1500
Flash disk 120
Camera 680
Mobile 550
Apple Laptop 2200

More Related Content

What's hot

5 collection framework
5 collection framework5 collection framework
5 collection frameworkMinal Maniar
 
Java Collections
Java CollectionsJava Collections
Java Collectionsparag
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work pptRanjith Alappadan
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Java10 Collections and Information
Java10 Collections and InformationJava10 Collections and Information
Java10 Collections and InformationSoftNutx
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - JavaDrishti Bhalla
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaEdureka!
 
java collections
java collectionsjava collections
java collectionsjaveed_mhd
 
Collections in Java
Collections in JavaCollections in Java
Collections in JavaKhasim Cise
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection frameworkankitgarg_er
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Aijaz Ali Abro
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Java collections
Java collectionsJava collections
Java collectionsAmar Kutwal
 

What's hot (20)

5 collection framework
5 collection framework5 collection framework
5 collection framework
 
F sharp lists & dictionary
F sharp   lists &  dictionaryF sharp   lists &  dictionary
F sharp lists & dictionary
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work ppt
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Java collection
Java collectionJava collection
Java collection
 
Java10 Collections and Information
Java10 Collections and InformationJava10 Collections and Information
Java10 Collections and Information
 
Collections Api - Java
Collections Api - JavaCollections Api - Java
Collections Api - Java
 
Java.util
Java.utilJava.util
Java.util
 
Java ArrayList Tutorial | Edureka
Java ArrayList Tutorial | EdurekaJava ArrayList Tutorial | Edureka
Java ArrayList Tutorial | Edureka
 
java collections
java collectionsjava collections
java collections
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Java collections
Java collectionsJava collections
Java collections
 
Collections framework in java
Collections framework in javaCollections framework in java
Collections framework in java
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
22.collections(1)
22.collections(1)22.collections(1)
22.collections(1)
 

Similar to Array list(1)

oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionssuseredfbe9
 
COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptxSruthyPJ
 
Collections generic
Collections genericCollections generic
Collections genericsandhish
 
Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40bhawna sharma
 
A2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptA2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptRithwikRanjan
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.pptsoniya555961
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & CollectionArya
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & CollectionArya
 
java I am trying to run my code but it is not letting me .pdf
java    I am trying to run my code but it is not letting me .pdfjava    I am trying to run my code but it is not letting me .pdf
java I am trying to run my code but it is not letting me .pdfadinathassociates
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanZeeshan Khan
 

Similar to Array list(1) (20)

oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
Collection Framework-1.pptx
Collection Framework-1.pptxCollection Framework-1.pptx
Collection Framework-1.pptx
 
Generics collections
Generics collectionsGenerics collections
Generics collections
 
COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptx
 
Collections
CollectionsCollections
Collections
 
Collections generic
Collections genericCollections generic
Collections generic
 
Java collections
Java collectionsJava collections
Java collections
 
Collections lecture 35 40
Collections lecture 35 40Collections lecture 35 40
Collections lecture 35 40
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
A2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.pptA2003822018_21789_17_2018_09. ArrayList.ppt
A2003822018_21789_17_2018_09. ArrayList.ppt
 
ArrayList.docx
ArrayList.docxArrayList.docx
ArrayList.docx
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & Collection
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & Collection
 
java I am trying to run my code but it is not letting me .pdf
java    I am trying to run my code but it is not letting me .pdfjava    I am trying to run my code but it is not letting me .pdf
java I am trying to run my code but it is not letting me .pdf
 
Array properties
Array propertiesArray properties
Array properties
 
Collection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshanCollection framework (completenotes) zeeshan
Collection framework (completenotes) zeeshan
 
Presentation1
Presentation1Presentation1
Presentation1
 

More from abdullah619

Testing chapter updated (1)
Testing chapter updated (1)Testing chapter updated (1)
Testing chapter updated (1)abdullah619
 
Exception handling
Exception handlingException handling
Exception handlingabdullah619
 
4 oo inheritance in c# (1)
4   oo inheritance in c# (1)4   oo inheritance in c# (1)
4 oo inheritance in c# (1)abdullah619
 
3 instantiating an object in c# (1)
3  instantiating an object in c# (1)3  instantiating an object in c# (1)
3 instantiating an object in c# (1)abdullah619
 
Methods in c# (1)
Methods in c# (1)Methods in c# (1)
Methods in c# (1)abdullah619
 

More from abdullah619 (12)

Testing chapter updated (1)
Testing chapter updated (1)Testing chapter updated (1)
Testing chapter updated (1)
 
Queue
QueueQueue
Queue
 
Stacks
StacksStacks
Stacks
 
Lists
Lists Lists
Lists
 
Collections (1)
Collections (1)Collections (1)
Collections (1)
 
Exception handling
Exception handlingException handling
Exception handling
 
Polumorphism
PolumorphismPolumorphism
Polumorphism
 
Abstrcation
AbstrcationAbstrcation
Abstrcation
 
4 oo inheritance in c# (1)
4   oo inheritance in c# (1)4   oo inheritance in c# (1)
4 oo inheritance in c# (1)
 
3 instantiating an object in c# (1)
3  instantiating an object in c# (1)3  instantiating an object in c# (1)
3 instantiating an object in c# (1)
 
Methods in c# (1)
Methods in c# (1)Methods in c# (1)
Methods in c# (1)
 
5. linked list
5. linked list5. linked list
5. linked list
 

Recently uploaded

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 

Recently uploaded (20)

How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 

Array list(1)

  • 2. Collections Collection classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces. Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items on the basis of an index etc. These classes create collections of objects of the Object class, which is the base class for all data types in C#. Various Collection Classes and Their Usage The following are the some commonly used classes of the System.Collection namespace.
  • 3. Collections Class Description and Usage ArrayList It represents 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. Hashtable It uses a key to access the elements in the collection. A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection. SortedList It uses a key as well as an index to access the items in a list. A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key , it is a Hashtable. The collection of items is always sorted by the key value. Stack It represents a last-in, first out collection of object. It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. Queue It represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue and when you remove an item, it is called deque. BitArray It represents an array of the binary representation using the values 1 and 0. It is used when you need to store the bits but do not know the number of bits in advance. You can access items from the BitArray collection by using an integer index, which starts from zero.
  • 4. ArrayList  The ArrayList collection class is a conventional arrays and provides dynamic resizing of the collection. Method / Property Description Add Adds an object to the end of the ArrayList. Capacity Property that gets and sets the number of elements for which space is currently reserved in the ArrayList. Clear Removes all elements from the ArrayList. Contains Determines whether an element is in the ArrayList. Count Read-only property that gets the number of elements stored in the ArrayList. IndexOf Returns the zero-based index of the first occurrence of a value in the ArrayList Insert Inserts an element into the ArrayList at the specified index. Remove Removes the first occurrence of a specific object from the ArrayList. RemoveAt Removes the element at the specified index of the ArrayList. TrimToSize Sets the capacity to the actual number of elements in the ArrayList.
  • 5. 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.
  • 6. Methods of the ArrayList class: Sr. No. Methods 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.
  • 7. Example The following example demonstrates the concept: You have been requested to develop an application which maintain set of items and prices during run time: Sharaf DG needs a program to store Items and its Prices. They want to have the options to sort by items’ name as well to sort prices. Design an interface similar to the following
  • 8. Example : Interface Design Tools names are labeled as shown in below figure. txtItem txtPrice btnAdd btnDP btnON btnAPbtnOP btnAN
  • 9. Example : Coding Validate entered price: is double Add Names & Prices to the ArrayLits Convert to double data typeConvert to double data type Use collection library
  • 10. Example : Coding Validate entered price: is double Add Names & Prices to the ArrayLits Convert to double data typeConvert to double data type
  • 11. Example : Coding Displays items names 1. The original list of names 2. The ascending sorted names 3. The descending sorted names
  • 12. Example : Coding Displays items Prices 1. The original list of Prices 2. The ascending sorted Prices 3. The descending sorted Prices
  • 13. Example : Runtime Enter these values with the same sequence TV 1500 Flash disk 120 Camera 680 Mobile 550 Apple Laptop 2200
  • 14. Example : Runtime Enter these values with the same sequence TV 1500 Flash disk 120 Camera 680 Mobile 550 Apple Laptop 2200

Editor's Notes

  1. Collections are data structures that holds data in different ways for flexible operations.