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