Home

Introduction

 

Fundamentals

 

Introduction

 

 

     
 
 

 

   
   
 

Previous Copyright © 2015 FunctionX Next

Home

Built-In Interfaces: IList

     

Interfaces and Collections

 

Starting a Collection

A collection is a group of items. All the items in the (same) group must be of the same type. To give you the foundation for (or to create) a collection, the .NET Framework provides an interface named ICollection. Actually, that interface doesn't provide much functionality. Its primary role is to let you know the number of items in a collection. To do this, the interface is equipped with a (read-only) property named Count:

abstract Count : int with get

While a collection is being created or for a collection that exists already, this property holds the number of items in the collection. The only operation the ICollection interface provides is the ability to copy the items from its collection to another type of collection (called an array).

Iterating Through a Collection

Once a collection exists, the most routine operation is to access each one of its items or members, for any reason. To support this operation, the .NET Framework provides an interface named IEnumerator:

type IEnumerator =
    interface
    end

The IEnumerator interface allows you to visit an item, do anything on it, and move to the next item in the collection. It does this through a property (named Current) and two methods (named MoveNext and Reset). Normally, you will not need to use that property or to call those methods. As we will see in this lesson, the operations are performed behind the scenes for you. You may be concerned with the IEnumerator interface only if you are creating your own collection class. Fortunately, both the .NET Framework and the F# language provide all the collection classes we need for our lessons.

Enumerating a Collection

In order to iterate through a collection, you use an object that holds a reference to the iterator. The main role of such an object is to indicate whether iteration is possible or available on the collection. To let you get such a reference, the .NET Framework provides an interface named IEnumerable:

type IEnumerable =
    interface
    end

The IEnumerable interface is equipped with only one method, named GetEnumerator. Its signature is:

abstract GetEnumerator : unit -> IEnumerator

As you may imagine, the role of the IEnumerable interface is to check whether iteration is available in a certain collection and, if so, to provide an object that can perform iteration.

Introduction to Lists and Collections

In the .NET Framework, the primary interface used to create a collection is called IList. It implements both the ICollection and the IEnumerable interfaces:

type IList =
    interface
        interface ICollection
        interface IEnumerable
    end

The IList interface provides its implementers with all the primary properties and routine methods necessary for a collection class. In many cases, you will not need to create your own collection classes (although you can). Both the F# language and especially the .NET framework provide collection classes for most scenarios you can think of. As a matter of fact, there are many collection-based controls that already support collections using appropriate properties. The classes of most of those controls are equipped with a property named Items. That's the case for the list box. The Items property for the ListBox is based on a custom class named ObjectCollection. It implements the IList, the ICollection, and the IEnumerable interface:

type ObjectCollection =
    class
        interface IList
        interface ICollection
        interface IEnumerable
    end

As mentioned already, the IList interface is equipped to perform all necessary operations of a collection:


Home Copyright © 2014-2015 FunctionX Home