SlideShare a Scribd company logo
1 of 5
Collections
• A Collection is a group of items or objects put together
• A Book collection is a number of books grouped or put together on a shelf
• A Collection in programming has similar meaning
• Gathering number of objects together, for example collecting names of all
student in a list
• More formally a Collection in programming is a class that allows
putting objects together
• Collection classes also allow methods to manage the objects within
the collection, such as, addition, removal, search etc.
Saifut
1
Collections
• An Array can be used to group a bunch of objects together
• Disadvantages of using an Array
• Array is fixed size. It cannot grow or shrink
• int[] numbers = new int[100]; // size of the numbers array is fixed to 100
• Programmer has to manage the collection, such as, adding, removing,
searching and moving items within the array etc.
• Collection classes removes the above disadvantages
• To work with Collection classes in C# the following namespaces have
to be included in your project:
• Systems.Collections
• Systems.Collections.Generic
Saifut
2
Collections
• Classes within the System.Collection namespace do not check the type of object
in the collection
• Example of some of these classes are:
• ArrayList, Stack, Queue
• Classes within the System.Collection.Generic namespace check the type of object
in the collection
• Following example shows difference between using the two namespaces
System.Collection System.Collection.Generic
Saifut
3
ArrayList scList = ArrayList(); List<string> scgList = new List<string>();
scList.Add(“saif”); scgList.Add(“saif”);
scList.Add(32); scgList.Add(32); // Error
// scList allows strings and ints //csgList only allows strings to be added to the
// to be added to the list. No Type // List. This is called type checking.
// checking.
Collections
• Generic classes enforce type checking.
• When using a Generic class the type of the object is specified as a type parameter
List<string>
• Type parameter appears within the <> brackets
• The type of object in the example above is string. So this list can only store
strings.
• Some of the collections that are part of the System.Collection.Generic
namespace are:
List<T>, LinkedList<T>, Stack<T>, Queue<T>
<T> refers to the type parameter. In the example above T = string.
Saifut
4
Collections
• The collection classes we will be using are:
• Note that we will only use the Generic Stack and Queue collections. Also note
ArrayList is not generic. Even though it is covered we should use List<T> instead.
Saifut
5
Collection Class Description Namespace
List<T> Stores objects that can be accessed by index, like arrays. List
can dynamically grow.
System.Collection.Generic
ArrayList Similar to list. Consider using List instead. System.Collection
LinkedList<T> Implements List as a collection of nodes linked together. The list
is a doubly-linked list.
System.Collection.generic
Stack<T> Collection of objects inserted and retrieved in Last-In-First-Out
order.
System.Collection.Generic
Queue<T> Collection of objects inserted and retrieved in First-In-First-Out
order.
System.Collection.Generic

More Related Content

What's hot (20)

Lists
Lists Lists
Lists
 
Loops and arrays
Loops and arraysLoops and arrays
Loops and arrays
 
Windows controls in c
Windows controls in cWindows controls in c
Windows controls in c
 
Javasession7
Javasession7Javasession7
Javasession7
 
ArrayList in JAVA
ArrayList in JAVAArrayList in JAVA
ArrayList in JAVA
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
 
Javascript ADT - List
Javascript   ADT - ListJavascript   ADT - List
Javascript ADT - List
 
Java Tutorial Lab 3
Java Tutorial Lab 3Java Tutorial Lab 3
Java Tutorial Lab 3
 
Data structure
Data structureData structure
Data structure
 
Object Class
Object ClassObject Class
Object Class
 
Java Tutorial Lab 4
Java Tutorial Lab 4Java Tutorial Lab 4
Java Tutorial Lab 4
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Array
ArrayArray
Array
 
Data structures2
Data structures2Data structures2
Data structures2
 
Hash based inventory system
Hash based inventory systemHash based inventory system
Hash based inventory system
 
Ts project Hash based inventory system
Ts project Hash based inventory systemTs project Hash based inventory system
Ts project Hash based inventory system
 
Dictionaries
DictionariesDictionaries
Dictionaries
 
Cis166 final review c#
Cis166 final review c#Cis166 final review c#
Cis166 final review c#
 
Cis166 Final Review C#
Cis166 Final Review C#Cis166 Final Review C#
Cis166 Final Review C#
 
Algorithm and Programming (Searching)
Algorithm and Programming (Searching)Algorithm and Programming (Searching)
Algorithm and Programming (Searching)
 

Similar to Understanding Collections in C

Similar to Understanding Collections in C (20)

9collection in c#
9collection in c#9collection in c#
9collection in c#
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_Framework
 
Lists
ListsLists
Lists
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
Java Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxJava Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptx
 
C# Non generics collection
C# Non generics collectionC# Non generics collection
C# Non generics collection
 
COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptx
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
ch06.ppt
ch06.pptch06.ppt
ch06.ppt
 
array Details
array Detailsarray Details
array Details
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
collections
 collections collections
collections
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
 
collection framework.pptx
collection framework.pptxcollection framework.pptx
collection framework.pptx
 
Collections Training
Collections TrainingCollections Training
Collections Training
 
Data structures
Data structuresData structures
Data structures
 

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 (10)

Testing chapter updated (1)
Testing chapter updated (1)Testing chapter updated (1)
Testing chapter updated (1)
 
Queue
QueueQueue
Queue
 
Stacks
StacksStacks
Stacks
 
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

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
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
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
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
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
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
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
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
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
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
 

Recently uploaded (20)

The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
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...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
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)
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
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...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
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
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
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...
 

Understanding Collections in C

  • 1. Collections • A Collection is a group of items or objects put together • A Book collection is a number of books grouped or put together on a shelf • A Collection in programming has similar meaning • Gathering number of objects together, for example collecting names of all student in a list • More formally a Collection in programming is a class that allows putting objects together • Collection classes also allow methods to manage the objects within the collection, such as, addition, removal, search etc. Saifut 1
  • 2. Collections • An Array can be used to group a bunch of objects together • Disadvantages of using an Array • Array is fixed size. It cannot grow or shrink • int[] numbers = new int[100]; // size of the numbers array is fixed to 100 • Programmer has to manage the collection, such as, adding, removing, searching and moving items within the array etc. • Collection classes removes the above disadvantages • To work with Collection classes in C# the following namespaces have to be included in your project: • Systems.Collections • Systems.Collections.Generic Saifut 2
  • 3. Collections • Classes within the System.Collection namespace do not check the type of object in the collection • Example of some of these classes are: • ArrayList, Stack, Queue • Classes within the System.Collection.Generic namespace check the type of object in the collection • Following example shows difference between using the two namespaces System.Collection System.Collection.Generic Saifut 3 ArrayList scList = ArrayList(); List<string> scgList = new List<string>(); scList.Add(“saif”); scgList.Add(“saif”); scList.Add(32); scgList.Add(32); // Error // scList allows strings and ints //csgList only allows strings to be added to the // to be added to the list. No Type // List. This is called type checking. // checking.
  • 4. Collections • Generic classes enforce type checking. • When using a Generic class the type of the object is specified as a type parameter List<string> • Type parameter appears within the <> brackets • The type of object in the example above is string. So this list can only store strings. • Some of the collections that are part of the System.Collection.Generic namespace are: List<T>, LinkedList<T>, Stack<T>, Queue<T> <T> refers to the type parameter. In the example above T = string. Saifut 4
  • 5. Collections • The collection classes we will be using are: • Note that we will only use the Generic Stack and Queue collections. Also note ArrayList is not generic. Even though it is covered we should use List<T> instead. Saifut 5 Collection Class Description Namespace List<T> Stores objects that can be accessed by index, like arrays. List can dynamically grow. System.Collection.Generic ArrayList Similar to list. Consider using List instead. System.Collection LinkedList<T> Implements List as a collection of nodes linked together. The list is a doubly-linked list. System.Collection.generic Stack<T> Collection of objects inserted and retrieved in Last-In-First-Out order. System.Collection.Generic Queue<T> Collection of objects inserted and retrieved in First-In-First-Out order. System.Collection.Generic