1 / 17

From C++ to C#

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.

craig
Download Presentation

From C++ to C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. From C++ to C# Part 2

  2. List class • Section 1.6.7 List class • Illustrates new features of C# such as Properties, Indexers and Events

  3. List class Fields • Refer 1.6.7

  4. List class constructor • Refer 1.6.7

  5. 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)

  6. 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#)

  7. 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).

  8. 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

  9. Read-Write Property • public int Capacity { • get { • return items.Length; • }

  10. Read-Write Property • Set accessor has a single implicit parameter named value and no return type

  11. 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; • } • } • }

  12. 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

  13. 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.

  14. 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();}

  15. 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.

  16. Indexer • public T this[int index] { • get { • return items[index]; • } • set { • items[index] = value; • OnChanged(); • } • }

  17. Assignment • Try out list class and test it out using some test programs. .

More Related