Professional Documents
Culture Documents
Lecture 7
Lecture 7
Lecture 7
Jawad Rafeeq
Jawadrafeeq@ciitvehari.edu.pk
Outline
1D Array
• Array as a collection of variables of the same type stored at
contiguous memory locations.
• In C# the allocation of memory for the arrays is done dynamically but
can explicitly define the size.
• Arrays are kind of objects, therefore it is easy to find their size using
the predefined functions.
• int[] intArray;
• Int Array = new int[5];
It is a reference, which does not have a value (it points to null heap).
1D Array
• Array as a collection of variables of the same type stored at
contiguous memory locations.
• In C# the allocation of memory for the arrays is done dynamically but
can explicitly define the size.
• Arrays are kind of objects, therefore it is easy to find their size using
the predefined functions.
• int[] intArray;
• Int Array = new int[5];
// foreach loop begin it will run till the last element of the array
• using System.Collections.Generic;
Creating List<T> object
• Generic Form:
• List<T> Abc = new List<T>();
• // List with default capacity
• List<Int16> Abc = new List<Int16>();
• // List with capacity = 5
• List<string> authors = new List<string>(5);
• List<string> animalsList = new List<string>(animals);
Adding Items to a C# List
• // Dynamic ArrayList with no size limit // List of string
• List<int> numberList = new List<int>(); List<string> authors = new List<string>(5);
• numberList.Add(32); authors.Add("Bruce Hed");
authors.Add("Chris Love");
• numberList.Add(21); authors.Add("Allen O'neill");
• numberList.Add(45); authors.Add("Alice mark");
• numberList.Add(11); authors.Add("SD wood");
• numberList.Add(89); authors.Add("David McCarter");
Adding Items to a C# List
// Collection of string
string[] animals = { "Cow", "Camel", "Elephant" };
// Create a List and add a collection
List<string> animalsList = new List<string>();
animalsList.AddRange(animals);
foreach (string a in animalsList)
Console.WriteLine(a);
• The RemoveAt method removes an item at the given position. The following
code snippet removes the item at the 3rd position.
• authors.RemoveAt(3);
• The RemoveRange method removes a list of items from the starting index to
the number of items. The following code snippet removes two items starting
at 3rd position.
• authors.RemoveRange(3, 2);
• The Clear method removes all items from a List<T>. The following code
snippet removes all items from a List.
• authors.Clear();
Find an Item in a C# List
Simple Method to find a element in List.
int index = animalsList.IndexOf("Camel");
if (index > -1)
Console.WriteLine("Item is in the List "+index);
else
Console.WriteLine("Item not found");
• For example, the following code snippet finds a string starting at the 3rd position in a String.
• Console.WriteLine(authors.IndexOf(“Fox", 2));
• The following code snippet looks for a string in the backward direction and returns the index of the
item if found.
• Console.WriteLine(authors.LastIndexOf(“Camel"));
Sort a C# List Items
The Sort method of List<T> sorts all items of the List using the QuickSort algorithm.
• }
How to confuse C# Lists?