Chapter 7c
Chapter 7c
Collections/Classes
Classes
Classes
– All data types in C# are really instances of
classes.
Strings are a good example of the abilities of classes
with all of the methods available to anything that is typed
a string
– Classes are structures that combine data and
processing into one component and are the basis
of object oriented programming
Classes in C#
4
ArrayList Class (continued)
5
Table: ArrayList members
ArrayList Class (continued)
6
Table: ArrayList members (continued)
ArrayList Class (continued)
8
ArrayList Class (continued)
using System.Collections;
ArrayList anArray = new ArrayList(); // Instantiates ArrayList
anArray.Add("Today is the first day of the rest of your life!");
anArray.Add("Live it to the fullest!");
anArray.Add("ok");
anArray.Add("You may not get a second chance.");
anArray.RemoveAt(2); // Removes the third physical one
for (int i = 0; i < anArray.Count; i++) //Displays elements
{
Console.WriteLine(anArray[i]);
}
Console.ReadKey();
}
9
ArrayList Class (continued)
10
Array vs Arraylist
Array ArrayList
– Fixed length. Once defined – Variable Length. One can
the size cannot be add and remove elements
changed. from the array.
– Elements must all be of the – Elements can be of different
same base type. (That is, types, but this comes at an
Strongly Typed!!) overhead.
– Much faster processing – Elements are all “typed” as
than ArrayList since it does base Object and then must
not have to worry about be cast to the correct data
data type nor tracking adds type.
and removes. List has similar properties to
ArrayList, but is strongly
typed.
Other Collection Classes
12
BitArray class
14
HashTable class
16
HashTable class (continued)
17