SlideShare a Scribd company logo
Generic Collections 
In our previous article we saw on generics and how we separated the data-type logic from a 
logical code snippet using generics. Taking forward that same logic, means separating collection 
logic i.e (Add, Search, Remove, Clear) from data-type, we will see different types of generic 
collections available in .net. 
We can use generic collections in our code by calling the namespace "using 
System.Collections.Generic". 
Generic Collections helps us to create flexible type-safe, strong type collections at compile time. 
Generic collections helps us to separate the collection logic (Add, Search, Remove, Clear) from 
different data-types available in .net. 
Why Generic Collections 
There are already different types of dotnet collections available in .net like array, arraylist, 
hashtables and specialized collections (string collections, hybrid collections) each of these 
collections have their own strong point and weak point.
For example: 
Arrays are strong types so there are no boxing and unboxing but weak point of this is it of fixed 
length size means it is not flexible. 
Arraylists and Hashtables are flexible in size but they are not strong type collections, we can pass 
any data-type objects to it, means there are lots of boxing and unboxing which means slow in 
performance. 
So by keeping in this mind dotnet development team has brought Generic Collections which 
bridges advantages of both strong type and dynamically resize and at a same time to pass any 
data-type to collections logic. 
Development team applied generic concept on dotnet collections 
 .NET collections to make generic collections. 
 Arraylist to make list generic collections. 
 Hashtables to make Dictionary. 
 Stacks and Queues to make Stacks and Queues generics. 
All these above collections available in "using System.Collections.Generic" namespace.
Types of Generic Collections 
Generic List Collection 
List collection are strong type index based collection and dynamically resizable and at a same 
time we can pass any data type to a list object. It provides many inbuilt methods to manipulate 
list object. List generic collections is a generic concept applied over Arrays and Arraylist. 
Syntax 
List obj = new List(); 
Where "T" generic parameter you can pass any data-type or custom class object to this 
parameter. 
Example 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
namespace GenericCollections 
{ 
class Program 
{ 
static void Main(string[] args) 
{ 
List list = new List(); 
list.Add(1); 
list.Add(2); 
list.Add(9); 
foreach (int numbers in list) 
{ 
Console.WriteLine(numbers); 
} 
Console.WriteLine("Count -> {0}",list.Count); 
Console.WriteLine("n------------------------------------------- 
n"); 
List list1 = new List(); 
list1.Add(false); 
list1.Add(true); 
list1.Add(true);
list1.Add(false); 
foreach (bool booleans in list1) 
{ 
Console.WriteLine(booleans); 
} 
Console.WriteLine("Count -> {0}", list1.Count); 
Console.WriteLine("n------------------------------------------- 
n"); 
List list2 = new List(); 
list2.Add("Khadak"); 
list2.Add("Shiv"); 
list2.Add("Shaam"); 
list2.Add("Pradeep Dhuri"); 
foreach (string stringnames in list2) 
{ 
Console.WriteLine(stringnames); 
} 
Console.WriteLine("Count -> {0}", list2.Count); 
Console.WriteLine("n------------------------------------------- 
n"); 
} 
} 
} 
Output
Generic Dictionary in C-sharp 
Dictionary is a generic collections which works on key and value pairs. Both key and value pair 
can have different data-types or same data-types or any custom types (i.e. class objects). 
Dictionary generic collections is a generic concept applied over Hashtable and it provides fast 
lookups with keys to get values. 
Syntax 
Dictionary obj = new List(); 
Dictionary Represents a collection of keys and values. 
Where "TKey" and "TValue" are generic parameters you can pass any data-type or custom class 
object to this parameters. 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
namespace GenericCollections 
{ 
class Program 
{ 
static void Main(string[] args) 
{ 
Dictionary animaldictionary = new Dictionary(); 
animaldictionary.Add("Tiger", 3); 
animaldictionary.Add("Lion", 2); 
animaldictionary.Add("Panda", 1); 
foreach (KeyValuePair pair in animaldictionary) 
{ 
Console.WriteLine("{0}, {1}", pair.Key, pair.Value); 
} 
Console.WriteLine("n------------------------------------------- 
n"); 
foreach (var pair in animaldictionary) 
{ 
Console.WriteLine("{0}, {1}", pair.Key, pair.Value); 
}
} 
} 
} 
Output 
Generic Stack and Queue 
Generic Stack and Queue is a concept applied over dot net collection stack and queue. Generic 
Stack and Queue naming coventions are similar to dot net collection stack and queue but it has 
got additional generic parameter "" to pass any data-type. Generic Stack will represents a last-in-first- 
out (LIFO) principle from collection of objects and Generic Queue will represents a first-in, 
first-out (FIFO) principle from collection of objects. Means when you remove any item from 
generic stack collections then the last or recently or new added item will be removed first like it 
will work in reverse order. While when you remove any item from generic queue collection then 
the first added item will be removed from generic queue collections. 
Stack works on (Last in first out principle - LIFO) on principle and uses "Push()" method to add 
items to the collections and "Pop()" method to remove any item from the collections.
Queue works on (First in First out - FIFO) principle and uses "Enqueue()" method to add items 
to the collections and "Dequeue()" method to remove any item from the collections. 
Syntax 
Stack objstack = new Stack();
Queue objqueue = new Queue (); 
Example 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
namespace GenericCollections 
{ 
class Program 
{ 
static void Main(string[] args) 
{ 
//Declaring Stack 
Stack stacknum = new Stack(); 
stacknum.Push(1); 
stacknum.Push(2); 
stacknum.Push(3); 
stacknum.Push(5); 
stacknum.Push(6); 
foreach (int numbers in stacknum) 
{ 
Console.WriteLine("Stack Numbers {0}", numbers); 
} 
stacknum.Pop(); //It will remove last/recent added element from 
the collections i.e (6) 
Console.WriteLine("n------------------------------------------- 
n"); 
//Declaring Stack 
Stack stacknames = new Stack(); 
stacknames.Push("Pradeep Dhuri"); 
stacknames.Push("Shiv Prasad"); 
stacknames.Push("Khadak"); 
stacknames.Push("Shaam"); 
foreach (string strnames in stacknames) 
{ 
Console.WriteLine("Stack Names {0}", strnames); 
} 
stacknames.Pop(); //It will remove last element/recent added 
element from the collections i.e. Shaam
Console.WriteLine("n------------------------------------------- 
n"); 
//Declaring Queue 
Queue Queuenum = new Queue(); 
Queuenum.Enqueue(1); 
Queuenum.Enqueue(2); 
Queuenum.Enqueue(3); 
Queuenum.Enqueue(5); 
Queuenum.Enqueue(6); 
foreach (int numbers in Queuenum) 
{ 
Console.WriteLine("Queue Numbers {0}", numbers); 
} 
Queuenum.Dequeue(); //It will remove first element from the 
collections i.e (1) 
Console.WriteLine("n------------------------------------------- 
n"); 
//Declaring Queues 
Queue Queuenames = new Queue(); 
Queuenames.Enqueue("Mohan Aiyer"); 
Queuenames.Enqueue("Shiv Prasad"); 
Queuenames.Enqueue("Khadak"); 
Queuenames.Enqueue("Shaam"); 
foreach (string strnames in Queuenames) 
{ 
Console.WriteLine("Queue Names {0}", strnames); 
} 
Queuenum.Dequeue(); //It will remove first added element from the 
collections i.e Mohan Aiyer 
Console.WriteLine("n------------------------------------------- 
n"); 
} 
} 
}

More Related Content

What's hot

L11 array list
L11 array listL11 array list
L11 array list
teach4uin
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
Riccardo Cardin
 
Collections framework
Collections frameworkCollections framework
Collections framework
Anand Buddarapu
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
Minal Maniar
 
07 java collection
07 java collection07 java collection
07 java collection
Abhishek Khune
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
talha ijaz
 
Array vs array list
Array vs array listArray vs array list
Array vs array list
Ravi Shetye
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
kumar gaurav
 
Java ArrayList Video Tutorial
Java ArrayList Video TutorialJava ArrayList Video Tutorial
Java ArrayList Video Tutorial
Marcus Biel
 
Laporan Resmi Algoritma dan Struktur Data :
Laporan Resmi Algoritma dan Struktur Data : Laporan Resmi Algoritma dan Struktur Data :
Laporan Resmi Algoritma dan Struktur Data : Siska Amelia
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
Syed Afaq Shah MACS CP
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
Shalabh Chaudhary
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
 
Java Collections
Java  Collections Java  Collections
collections
collectionscollections
collections
javeed_mhd
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
Prof. Erwin Globio
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
Khasim Cise
 
Any Which Array But Loose
Any Which Array But LooseAny Which Array But Loose
Any Which Array But Loose
michael.labriola
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
Sony India Software Center
 

What's hot (20)

L11 array list
L11 array listL11 array list
L11 array list
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
Collections framework
Collections frameworkCollections framework
Collections framework
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
07 java collection
07 java collection07 java collection
07 java collection
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Array vs array list
Array vs array listArray vs array list
Array vs array list
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Java ArrayList Video Tutorial
Java ArrayList Video TutorialJava ArrayList Video Tutorial
Java ArrayList Video Tutorial
 
Laporan Resmi Algoritma dan Struktur Data :
Laporan Resmi Algoritma dan Struktur Data : Laporan Resmi Algoritma dan Struktur Data :
Laporan Resmi Algoritma dan Struktur Data :
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
collections
collectionscollections
collections
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
Any Which Array But Loose
Any Which Array But LooseAny Which Array But Loose
Any Which Array But Loose
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 

Similar to Collections generic

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
 
Net framework session02
Net framework session02Net framework session02
Net framework session02
Vivek chan
 
Generics collections
Generics collectionsGenerics collections
Generics collections
Yaswanth Babu Gummadivelli
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
BG Java EE Course
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collectionsphanleson
 
collections
 collections collections
collection framework in java
collection framework in javacollection framework in java
collection framework in java
MANOJ KUMAR
 
Array list(1)
Array list(1)Array list(1)
Array list(1)
abdullah619
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & Collection
Arya
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & Collection
Arya
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
Nguync91368
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_FrameworkKrishna Sujeer
 
Java.util
Java.utilJava.util
Java.util
Ramakrishna kapa
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
soniya555961
 
ArrayList.docx
ArrayList.docxArrayList.docx
ArrayList.docx
veerendranath12
 
collectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptxcollectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptx
hemanth248901
 

Similar to Collections generic (20)

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)
 
Net framework session02
Net framework session02Net framework session02
Net framework session02
 
Generics collections
Generics collectionsGenerics collections
Generics collections
 
Collections
CollectionsCollections
Collections
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Generics Collections
Generics CollectionsGenerics Collections
Generics Collections
 
collections
 collections collections
collections
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
Array list(1)
Array list(1)Array list(1)
Array list(1)
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & Collection
 
Generic Programming & Collection
Generic Programming & CollectionGeneric Programming & Collection
Generic Programming & Collection
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_Framework
 
Java.util
Java.utilJava.util
Java.util
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
 
ArrayList.docx
ArrayList.docxArrayList.docx
ArrayList.docx
 
collectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptxcollectionframework-141116005344-conversion-gate01.pptx
collectionframework-141116005344-conversion-gate01.pptx
 
Array properties
Array propertiesArray properties
Array properties
 
Presentation1
Presentation1Presentation1
Presentation1
 

Recently uploaded

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 

Recently uploaded (20)

The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 

Collections generic

  • 1. Generic Collections In our previous article we saw on generics and how we separated the data-type logic from a logical code snippet using generics. Taking forward that same logic, means separating collection logic i.e (Add, Search, Remove, Clear) from data-type, we will see different types of generic collections available in .net. We can use generic collections in our code by calling the namespace "using System.Collections.Generic". Generic Collections helps us to create flexible type-safe, strong type collections at compile time. Generic collections helps us to separate the collection logic (Add, Search, Remove, Clear) from different data-types available in .net. Why Generic Collections There are already different types of dotnet collections available in .net like array, arraylist, hashtables and specialized collections (string collections, hybrid collections) each of these collections have their own strong point and weak point.
  • 2. For example: Arrays are strong types so there are no boxing and unboxing but weak point of this is it of fixed length size means it is not flexible. Arraylists and Hashtables are flexible in size but they are not strong type collections, we can pass any data-type objects to it, means there are lots of boxing and unboxing which means slow in performance. So by keeping in this mind dotnet development team has brought Generic Collections which bridges advantages of both strong type and dynamically resize and at a same time to pass any data-type to collections logic. Development team applied generic concept on dotnet collections  .NET collections to make generic collections.  Arraylist to make list generic collections.  Hashtables to make Dictionary.  Stacks and Queues to make Stacks and Queues generics. All these above collections available in "using System.Collections.Generic" namespace.
  • 3. Types of Generic Collections Generic List Collection List collection are strong type index based collection and dynamically resizable and at a same time we can pass any data type to a list object. It provides many inbuilt methods to manipulate list object. List generic collections is a generic concept applied over Arrays and Arraylist. Syntax List obj = new List(); Where "T" generic parameter you can pass any data-type or custom class object to this parameter. Example using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericCollections { class Program { static void Main(string[] args) { List list = new List(); list.Add(1); list.Add(2); list.Add(9); foreach (int numbers in list) { Console.WriteLine(numbers); } Console.WriteLine("Count -> {0}",list.Count); Console.WriteLine("n------------------------------------------- n"); List list1 = new List(); list1.Add(false); list1.Add(true); list1.Add(true);
  • 4. list1.Add(false); foreach (bool booleans in list1) { Console.WriteLine(booleans); } Console.WriteLine("Count -> {0}", list1.Count); Console.WriteLine("n------------------------------------------- n"); List list2 = new List(); list2.Add("Khadak"); list2.Add("Shiv"); list2.Add("Shaam"); list2.Add("Pradeep Dhuri"); foreach (string stringnames in list2) { Console.WriteLine(stringnames); } Console.WriteLine("Count -> {0}", list2.Count); Console.WriteLine("n------------------------------------------- n"); } } } Output
  • 5. Generic Dictionary in C-sharp Dictionary is a generic collections which works on key and value pairs. Both key and value pair can have different data-types or same data-types or any custom types (i.e. class objects). Dictionary generic collections is a generic concept applied over Hashtable and it provides fast lookups with keys to get values. Syntax Dictionary obj = new List(); Dictionary Represents a collection of keys and values. Where "TKey" and "TValue" are generic parameters you can pass any data-type or custom class object to this parameters. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericCollections { class Program { static void Main(string[] args) { Dictionary animaldictionary = new Dictionary(); animaldictionary.Add("Tiger", 3); animaldictionary.Add("Lion", 2); animaldictionary.Add("Panda", 1); foreach (KeyValuePair pair in animaldictionary) { Console.WriteLine("{0}, {1}", pair.Key, pair.Value); } Console.WriteLine("n------------------------------------------- n"); foreach (var pair in animaldictionary) { Console.WriteLine("{0}, {1}", pair.Key, pair.Value); }
  • 6. } } } Output Generic Stack and Queue Generic Stack and Queue is a concept applied over dot net collection stack and queue. Generic Stack and Queue naming coventions are similar to dot net collection stack and queue but it has got additional generic parameter "" to pass any data-type. Generic Stack will represents a last-in-first- out (LIFO) principle from collection of objects and Generic Queue will represents a first-in, first-out (FIFO) principle from collection of objects. Means when you remove any item from generic stack collections then the last or recently or new added item will be removed first like it will work in reverse order. While when you remove any item from generic queue collection then the first added item will be removed from generic queue collections. Stack works on (Last in first out principle - LIFO) on principle and uses "Push()" method to add items to the collections and "Pop()" method to remove any item from the collections.
  • 7. Queue works on (First in First out - FIFO) principle and uses "Enqueue()" method to add items to the collections and "Dequeue()" method to remove any item from the collections. Syntax Stack objstack = new Stack();
  • 8. Queue objqueue = new Queue (); Example using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericCollections { class Program { static void Main(string[] args) { //Declaring Stack Stack stacknum = new Stack(); stacknum.Push(1); stacknum.Push(2); stacknum.Push(3); stacknum.Push(5); stacknum.Push(6); foreach (int numbers in stacknum) { Console.WriteLine("Stack Numbers {0}", numbers); } stacknum.Pop(); //It will remove last/recent added element from the collections i.e (6) Console.WriteLine("n------------------------------------------- n"); //Declaring Stack Stack stacknames = new Stack(); stacknames.Push("Pradeep Dhuri"); stacknames.Push("Shiv Prasad"); stacknames.Push("Khadak"); stacknames.Push("Shaam"); foreach (string strnames in stacknames) { Console.WriteLine("Stack Names {0}", strnames); } stacknames.Pop(); //It will remove last element/recent added element from the collections i.e. Shaam
  • 9. Console.WriteLine("n------------------------------------------- n"); //Declaring Queue Queue Queuenum = new Queue(); Queuenum.Enqueue(1); Queuenum.Enqueue(2); Queuenum.Enqueue(3); Queuenum.Enqueue(5); Queuenum.Enqueue(6); foreach (int numbers in Queuenum) { Console.WriteLine("Queue Numbers {0}", numbers); } Queuenum.Dequeue(); //It will remove first element from the collections i.e (1) Console.WriteLine("n------------------------------------------- n"); //Declaring Queues Queue Queuenames = new Queue(); Queuenames.Enqueue("Mohan Aiyer"); Queuenames.Enqueue("Shiv Prasad"); Queuenames.Enqueue("Khadak"); Queuenames.Enqueue("Shaam"); foreach (string strnames in Queuenames) { Console.WriteLine("Queue Names {0}", strnames); } Queuenum.Dequeue(); //It will remove first added element from the collections i.e Mohan Aiyer Console.WriteLine("n------------------------------------------- n"); } } }