170 likes | 326 Views
From C++ to C#. Part 2. List class. Section 1.6.7 List class Illustrates new features of C# such as Properties, Indexers and Events. List class Fields. Refer 1.6.7. List class constructor. Refer 1.6.7 . List count.
E N D
From C++ to C# Part 2
List class • Section 1.6.7 List class • Illustrates new features of C# such as Properties, Indexers and Events
List class Fields • Refer 1.6.7
List class constructor • Refer 1.6.7
List count • Note that count field (not property which is Count) is initialized automatically to 0 and not changed by constructor. • count is used to keep track of the number of items added to the list (e.g. by using the Add method). • count is different from the length of the items array (which is given by items.Length)
Properties • Natural extension of fields • Syntax same for Properties and fields • Read-Only Property has only get accessor • Read-Write Property has get and set accessor methods • Write-Only Property has only set accessor method • Note that set is also called accessor and not mutator (in C#)
Read-Only Property • Count property of List class • public int Count { get { return count; }} • get has no parameters and has return value of the type of the property • Defines Count property with get accessor (only).
Read-Only Property • When a property is referenced in an expression the get method is invoked to compute the value of the property. • Usage: • List ll = new List(10); • … • Int j = ll.Count; // Invokes get accessor
Read-Write Property • public int Capacity { • get { • return items.Length; • }
Read-Write Property • Set accessor has a single implicit parameter named value and no return type
Read-Write Property • set { • if (value < count) value = count; • if (value != items.Length) { • T[] newItems = new T[value];Array.Copy(items, 0, newItems, 0, count); • items = newItems; • } • } • }
Read-Write Property • When a property is referenced as the target of an assignment or as the operand of ++ or --, the set accessor is invoked with an argument that provides the new value. • List <string> names = new List <string>();names.Capacity = 100; // Invokes set accessorinti = names.Count; // Invokes get accessorint j = names.Capacity; // Invokes get accessor
Why have Properties? • Enables class user to access data using simple syntax as if they were fields • but • allows class implementer to include validation via set accessor • And class implementer to include computation while returning value via get accessor • Data encapsulation objective is met with simplicity of syntax for class user.
Indexer • Enables objects (of a class) to be indexed the same way as an array • List <string> names = new List <string>();names.Add("Liz");names.Add("Martha");names.Add("Beth");for (inti = 0; i < names.Count; i++) { string s = names[i]; names[i] = s.ToUpper();}
Indexer • An indexer is declared like a property except that the name of the member is this followed by a parameter list written between the delimiters [ and ]. • The parameters are available in the accessor(s) of the indexer. • Similar to properties, indexers can be read-write, read-only, and write-only.
Indexer • public T this[int index] { • get { • return items[index]; • } • set { • items[index] = value; • OnChanged(); • } • }
Assignment • Try out list class and test it out using some test programs. .