SlideShare a Scribd company logo
1 of 18
Download to read offline
PROGRAMMING IN C#
LIST
ENGR. MICHEAL OGUNDERO
YOU CAN CONTACT ME VIA
PHONE - +2348087365099
SKYPE – MICHEAL OGUNDERO
Email – ogunderoayodeji@gmail.com
CONTENT 2
1
2
3
4
Introduction / Characteristics
of List<T>
Adding Items/Collection to
List
Reading/Retrieving Items from
List
List Class Properties
5
6
7
8
Inserting Items at a Position in
List
Removing Items from a
Position in List
Finding an Item in List
Finding an Item in List
A QUICK ONE FROM LAST CLASS
(ENUMERATIONS)
3
• What class of data type does an enum belong to?
• On a sheet of paper, write a code snippet that defines an enum of
Colors populated with the rainbow colors.
• Within the main method, write a code that prints out the string value
of any two(2) colors from the enum.
• Also print out all the members of the enum.
RESULTS
companyname.com
4
INTRODUCTION 5
• List<T> class represents the list of objects which can be
accessed by index.
• It comes under the System.Collection.Generic namespace.
• List class can be used to create a collection of different
types like integers, strings etc. List<T> class also provides
the methods to search, sort, and manipulate lists.
CHARACTERISTICS OF THE LIST<T> CLASS 6
• A List<T> can be resized dynamically but arrays cannot.
• List<T> class can accept null as a valid value for reference types and it
also allows duplicate elements.
• If the count becomes equal to capacity, then the capacity of the list is
increased automatically by reallocating the internal array.
• This class can use both equality and ordering comparer.
• List<T> class is not sorted by default and elements are accessed by
zero-based index.
CREATING A LIST 7
• The code creates a list of Int16 and a
list of string types.
• It also creates a List<T> object with
an existing collection when more
than 5 elements are added to
‘studentsList’, it automatically
expands.
// List with default capacity
List<Int16> numberList = new List<Int16>();
// list with capacity = 5
List<string> studentsList = new List<string>(5);
string[] students = { ”Lateef", ”Wilson", ”Elvis" };
List<string> studentsList = new List<string>(stud
ents);
ADDING ITEMS TO LIST 8
• The add method adds an element to a
list.
• The code snippet creates two List<T>
objects and adds integer and string
items to them respectively.
numberList.Add(32);
numberList.Add(21);
numberList.Add(45);
numberList.Add(11);
studentsList.Add(”Nmesoma Ngozi");
studentsList.Add(”Opirite Obi");
studentsList.Add(”Olurombi
Anuoluwapo");
studentsList.Add(”Anthony Isaiah");
ADDING COLLECTION TO LIST 9
• We can also add a
collection to a list. The
addrange method is used
to add a collection to a list.
public static void Main(string[] args)
{
// Collection of string
String[] extraStudents ={ ”Bernard Onimisi", ”Bolu
Fasina", ”Chidera Ofokansi" };
// create a list and add a collection
studentsList.AddRange(extraStudents);
foreach(string student in studentsList)
Console.WriteLine(student);
}
READING/RETRIEVING ITEMS FROM A LIST 10
• List is a collection of items.
• We can use a foreach loop to
loop through its items.
• The code snippet reads all items
of a list and displays on the
console.
• To retrieve an item at a specific
position in the list, we can use
the collection’s index.
foreach (string student in studentsList)
Console.WriteLine(student);
Console.WriteLine(studentsList[2]);
LIST CLASS PROPERTIES 11
Console.WriteLine("count: " + studentsList.Count);
Console.WriteLine("capacity: "+studentsList.Capacity);
• List class properties include
the following:
• Capacity – number of
elements List<T> can contain.
The default capacity of a
list<T> is 0.
• Count – number of elements
in List<T>.
ASSIGNMENT:
Find out the default capacity of a list
and how it keeps adding to the
capacity as more elements are added.
INSERTING ITEMS AT A POSTION IN LIST 12
• The Insert method of list class
inserts an object at a given
position.
• The first parameter of the
method is the 0th based
index in the list.
• The InsertRange method can
insert a collection at the given
position.
//inserting a single item in a list
studentsList.Insert(3, ”Samuel Onasanya");
// collection of new students
String[] lateComers = { ”Abdulhaq
Ayo", ”Nicole", ”Tonade Oladapo" };
// insert array at position 2
studentsList.InsertRange(2, lateComers);
REMOVING ITEMS FROM A POSTION IN LIST 13
// Remove an item
studentsList.Remove(”Nicole");
// remove 3rd item
studentsList.RemoveAt(3);
// Remove a range
studentsList.RemoveRange(3, 2);
// remove all items
studentsList.Clear();
• The Remove method removes the
first occurrence of the given item
in the list.
• The RemoveAt method removes
an item at the given position.
• The RemoveRange method
removes a list of items from the
starting index to the number of
items.
• The Clear method removes all
items from a list<T>.
REMOVING ITEMS FROM A POSTION IN LIST… 14
ASSIGNMENT:
Add a line of code that removes ALL occurrences
of a particular student.
HINT: Use the “RemoveAll” method with lambda
expressions.
Visit codingame.com to learn about “Lambda Expressions” before next class
and also make sure you practice the exercises.
Be ready to share ideas in the next class on this.
https://www.codingame.com/playgrounds/213/using-c-linq---a-practical-overview/lambda-
expressions
FINDING AN ITEM IN LIST 15
• The IndexOf method finds an
item in a list.
• The IndexOf method returns -1
if there are no items found in
the list.
• We can also specify the position
in a list where IndexOf method
can start searching from.
• The LastIndexOf method finds
an item from the end of list.
Int idx = studentsList.Indexof(”Chidera
Ofokansi");
If (idx > 0)
Console.WriteLine($"item index in list is: {idx}");
else
Console.WriteLine("item not found");
Console.Writeline(studentsList.IndexOf(”Chider
a Ofokansi", 4));
Console.WriteLine(studentsList.Lastindexof(“Ch
idera Ofokansi"));
ASSIGNMENT 16
Use the list built-in methods below to sort, search and reverse a list.
• SORT
• SEARCH
• REVERSE
SEE YOU NEXT CLASS 17
SORTING ALGORITHMS:
• BUBBLE SORT
• SELECTION SORT
• INSERTION SORT
Systems Engineering Department,
University of Lagos, Nigeria.
(234) 808-736-5099 Micheal Ogundero
Thank You

More Related Content

What's hot (20)

The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
 
Lesson11
Lesson11Lesson11
Lesson11
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
STACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data StructureSTACK ( LIFO STRUCTURE) - Data Structure
STACK ( LIFO STRUCTURE) - Data Structure
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Stacks, Queues, Deques
Stacks, Queues, DequesStacks, Queues, Deques
Stacks, Queues, Deques
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Python for Beginners(v3)
Python for Beginners(v3)Python for Beginners(v3)
Python for Beginners(v3)
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
1. 3 singly linked list insertion 2
1. 3 singly linked list   insertion 21. 3 singly linked list   insertion 2
1. 3 singly linked list insertion 2
 
Stack project
Stack projectStack project
Stack project
 
Python programming
Python programmingPython programming
Python programming
 
stack presentation
stack presentationstack presentation
stack presentation
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
Stacks
StacksStacks
Stacks
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
The Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterateThe Functional Programming Triad of fold, scan and iterate
The Functional Programming Triad of fold, scan and iterate
 
DSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notesDSA UNIT II ARRAY AND LIST - notes
DSA UNIT II ARRAY AND LIST - notes
 

Similar to C# List Tutorial - Learn List Methods

Data type list_methods_in_python
Data type list_methods_in_pythonData type list_methods_in_python
Data type list_methods_in_pythondeepalishinkar1
 
COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptxSruthyPJ
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methodsnarmadhakin
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfalivaisi1
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdfarshin9
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 
Please add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxPlease add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxStewartt0kJohnstonh
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queueRai University
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queueRai University
 
Introduction To Programming with Python-4
Introduction To Programming with Python-4Introduction To Programming with Python-4
Introduction To Programming with Python-4Syed Farjad Zia Zaidi
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonCeline George
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfmaheshkumar12354
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using Ccpjcollege
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docxajoy21
 
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptxRANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptxOut Cast
 

Similar to C# List Tutorial - Learn List Methods (20)

Data type list_methods_in_python
Data type list_methods_in_pythonData type list_methods_in_python
Data type list_methods_in_python
 
COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptx
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdf
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
 
Lists
ListsLists
Lists
 
Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Please add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docxPlease add-modify the following to the original code using C# 1- Delet.docx
Please add-modify the following to the original code using C# 1- Delet.docx
 
F sharp lists & dictionary
F sharp   lists &  dictionaryF sharp   lists &  dictionary
F sharp lists & dictionary
 
Lists
ListsLists
Lists
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Introduction To Programming with Python-4
Introduction To Programming with Python-4Introduction To Programming with Python-4
Introduction To Programming with Python-4
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
 
Chapter 15 Lists
Chapter 15 ListsChapter 15 Lists
Chapter 15 Lists
 
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptxRANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
 
Python Lists.pptx
Python Lists.pptxPython Lists.pptx
Python Lists.pptx
 

More from Micheal Ogundero

csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structuresMicheal Ogundero
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markersMicheal Ogundero
 
A simple program C# program
A simple program C# programA simple program C# program
A simple program C# programMicheal Ogundero
 
datatypes_variables_constants
datatypes_variables_constantsdatatypes_variables_constants
datatypes_variables_constantsMicheal Ogundero
 
2 robot types_classifications
2 robot types_classifications2 robot types_classifications
2 robot types_classificationsMicheal Ogundero
 
c# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventionsc# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming ConventionsMicheal Ogundero
 
csharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_referencecsharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_referenceMicheal Ogundero
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesMicheal Ogundero
 

More from Micheal Ogundero (13)

static methods
static methodsstatic methods
static methods
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structures
 
selection structures
selection structuresselection structures
selection structures
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markers
 
A simple program C# program
A simple program C# programA simple program C# program
A simple program C# program
 
c# operators
c# operatorsc# operators
c# operators
 
datatypes_variables_constants
datatypes_variables_constantsdatatypes_variables_constants
datatypes_variables_constants
 
2 robot types_classifications
2 robot types_classifications2 robot types_classifications
2 robot types_classifications
 
History of robots
History of robotsHistory of robots
History of robots
 
c# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventionsc# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventions
 
c# Enumerations
c# Enumerationsc# Enumerations
c# Enumerations
 
csharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_referencecsharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_reference
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data Types
 

Recently uploaded

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Recently uploaded (20)

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 

C# List Tutorial - Learn List Methods

  • 1. PROGRAMMING IN C# LIST ENGR. MICHEAL OGUNDERO YOU CAN CONTACT ME VIA PHONE - +2348087365099 SKYPE – MICHEAL OGUNDERO Email – ogunderoayodeji@gmail.com
  • 2. CONTENT 2 1 2 3 4 Introduction / Characteristics of List<T> Adding Items/Collection to List Reading/Retrieving Items from List List Class Properties 5 6 7 8 Inserting Items at a Position in List Removing Items from a Position in List Finding an Item in List Finding an Item in List
  • 3. A QUICK ONE FROM LAST CLASS (ENUMERATIONS) 3 • What class of data type does an enum belong to? • On a sheet of paper, write a code snippet that defines an enum of Colors populated with the rainbow colors. • Within the main method, write a code that prints out the string value of any two(2) colors from the enum. • Also print out all the members of the enum.
  • 5. INTRODUCTION 5 • List<T> class represents the list of objects which can be accessed by index. • It comes under the System.Collection.Generic namespace. • List class can be used to create a collection of different types like integers, strings etc. List<T> class also provides the methods to search, sort, and manipulate lists.
  • 6. CHARACTERISTICS OF THE LIST<T> CLASS 6 • A List<T> can be resized dynamically but arrays cannot. • List<T> class can accept null as a valid value for reference types and it also allows duplicate elements. • If the count becomes equal to capacity, then the capacity of the list is increased automatically by reallocating the internal array. • This class can use both equality and ordering comparer. • List<T> class is not sorted by default and elements are accessed by zero-based index.
  • 7. CREATING A LIST 7 • The code creates a list of Int16 and a list of string types. • It also creates a List<T> object with an existing collection when more than 5 elements are added to ‘studentsList’, it automatically expands. // List with default capacity List<Int16> numberList = new List<Int16>(); // list with capacity = 5 List<string> studentsList = new List<string>(5); string[] students = { ”Lateef", ”Wilson", ”Elvis" }; List<string> studentsList = new List<string>(stud ents);
  • 8. ADDING ITEMS TO LIST 8 • The add method adds an element to a list. • The code snippet creates two List<T> objects and adds integer and string items to them respectively. numberList.Add(32); numberList.Add(21); numberList.Add(45); numberList.Add(11); studentsList.Add(”Nmesoma Ngozi"); studentsList.Add(”Opirite Obi"); studentsList.Add(”Olurombi Anuoluwapo"); studentsList.Add(”Anthony Isaiah");
  • 9. ADDING COLLECTION TO LIST 9 • We can also add a collection to a list. The addrange method is used to add a collection to a list. public static void Main(string[] args) { // Collection of string String[] extraStudents ={ ”Bernard Onimisi", ”Bolu Fasina", ”Chidera Ofokansi" }; // create a list and add a collection studentsList.AddRange(extraStudents); foreach(string student in studentsList) Console.WriteLine(student); }
  • 10. READING/RETRIEVING ITEMS FROM A LIST 10 • List is a collection of items. • We can use a foreach loop to loop through its items. • The code snippet reads all items of a list and displays on the console. • To retrieve an item at a specific position in the list, we can use the collection’s index. foreach (string student in studentsList) Console.WriteLine(student); Console.WriteLine(studentsList[2]);
  • 11. LIST CLASS PROPERTIES 11 Console.WriteLine("count: " + studentsList.Count); Console.WriteLine("capacity: "+studentsList.Capacity); • List class properties include the following: • Capacity – number of elements List<T> can contain. The default capacity of a list<T> is 0. • Count – number of elements in List<T>. ASSIGNMENT: Find out the default capacity of a list and how it keeps adding to the capacity as more elements are added.
  • 12. INSERTING ITEMS AT A POSTION IN LIST 12 • The Insert method of list class inserts an object at a given position. • The first parameter of the method is the 0th based index in the list. • The InsertRange method can insert a collection at the given position. //inserting a single item in a list studentsList.Insert(3, ”Samuel Onasanya"); // collection of new students String[] lateComers = { ”Abdulhaq Ayo", ”Nicole", ”Tonade Oladapo" }; // insert array at position 2 studentsList.InsertRange(2, lateComers);
  • 13. REMOVING ITEMS FROM A POSTION IN LIST 13 // Remove an item studentsList.Remove(”Nicole"); // remove 3rd item studentsList.RemoveAt(3); // Remove a range studentsList.RemoveRange(3, 2); // remove all items studentsList.Clear(); • The Remove method removes the first occurrence of the given item in the list. • The RemoveAt method removes an item at the given position. • The RemoveRange method removes a list of items from the starting index to the number of items. • The Clear method removes all items from a list<T>.
  • 14. REMOVING ITEMS FROM A POSTION IN LIST… 14 ASSIGNMENT: Add a line of code that removes ALL occurrences of a particular student. HINT: Use the “RemoveAll” method with lambda expressions. Visit codingame.com to learn about “Lambda Expressions” before next class and also make sure you practice the exercises. Be ready to share ideas in the next class on this. https://www.codingame.com/playgrounds/213/using-c-linq---a-practical-overview/lambda- expressions
  • 15. FINDING AN ITEM IN LIST 15 • The IndexOf method finds an item in a list. • The IndexOf method returns -1 if there are no items found in the list. • We can also specify the position in a list where IndexOf method can start searching from. • The LastIndexOf method finds an item from the end of list. Int idx = studentsList.Indexof(”Chidera Ofokansi"); If (idx > 0) Console.WriteLine($"item index in list is: {idx}"); else Console.WriteLine("item not found"); Console.Writeline(studentsList.IndexOf(”Chider a Ofokansi", 4)); Console.WriteLine(studentsList.Lastindexof(“Ch idera Ofokansi"));
  • 16. ASSIGNMENT 16 Use the list built-in methods below to sort, search and reverse a list. • SORT • SEARCH • REVERSE
  • 17. SEE YOU NEXT CLASS 17 SORTING ALGORITHMS: • BUBBLE SORT • SELECTION SORT • INSERTION SORT
  • 18. Systems Engineering Department, University of Lagos, Nigeria. (234) 808-736-5099 Micheal Ogundero Thank You