Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This step-by-step article shows you how to implement a custom collection in Visual C#. The Microsoft .NET Framework base class libraries offer a formal definition of a collection System.Collections.ICollection interface.
Original product version: Visual C#
Original KB number: 307484
Implement the ICollection interface in a custom class
The ICollection interface inherits from the IEnumerable interface. The ICollection interface defines a CopyTo method and three read-only properties: IsSynchronized, SyncRoot, and Count. ICollection inherits the GetEnumerator method from the IEnumerable interface. A custom collection class should implement the ICollection interface.
To implement the ICollection interface, follow these steps:
In Visual C# .NET, create a Windows application.
In Solution Explorer, right-click the project name, point to Add, and then click Add Class to add a class module named CustomCollection.
Add the following sample code to the beginning of the class module to import the
System.Collectionnamespace:using System.Collections;Replace any other code in the module with the following sample code:
public class CustomCollection : ICollection { private int[] intArr = {1,5,9}; private int Ct; public CustomCollection() { Ct=3; } }For simplicity, the
CustomCollectionclass holds an array with three integer items and a count variable.Implement the
CopyTomethod, which takes an integer array and an index as parameters. This method copies the items in a collection to the array starting at the index that is passed. To implement this method, paste the following code after the publicCustomCollectionconstructor:void ICollection.CopyTo(Array myArr, int index) { foreach (int i in intArr) { myArr.SetValue(i,index); index = index+1; } }Implement the
GetEnumeratormethod, which is inherited by theICollectioninterface fromIEnumerable. TheGetEnumeratormethod returns anEnumeratorobject that can iterate through a collection. Paste the following sample code after theCopyTomethod:IEnumerator IEnumerable.GetEnumerator() { return new Enumerator(intArr); }To implement the three read-only properties, paste the following code after the
GetEnumeratormethod:// The IsSynchronized Boolean property returns True if the // collection is designed to be thread safe; otherwise, it returns False. bool ICollection.IsSynchronized { get { return false; } } // The SyncRoot property returns an object, which is used for synchronizing // the collection. This returns the instance of the object or returns the // SyncRoot of other collections if the collection contains other collections. object ICollection.SyncRoot { get { return this; } } // The Count read-only property returns the number // of items in the collection. int ICollection.Count { get { return Ct; } }
Implement an Enumerator object for the GetEnumerator method
This section shows you how to create an Enumerator class that can iterate through CustomCollection.
Paste the following sample code after the end class statement in your class module:
public class Enumerator : IEnumerator { private int[] intArr; private int Cursor; }Declare the
intArrprivate integer array to hold the elements of theCustomCollectionclass when theGetEnumeratormethod is called. TheCursorfield member holds the current position while enumerating.Add a constructor with
intArras a parameter and set the localintArrto this. Paste the following sample code after the member field's declaration:public Enumerator(int[] intarr) { this.intArr = intarr; Cursor = -1; }Implement the
ResetandMoveNextmethods. To do this, paste the following code after the constructor:void IEnumerator.Reset() { Cursor = -1; } bool IEnumerator.MoveNext() { if (Cursor < intArr.Length) Cursor++; return(!(Cursor == intArr.Length)); }Resetsets theCursorto -1 andMoveNextmoves theCursorto the next element.MoveNextreturns True if successful.Implement the
Currentread-only property that returns the item pointed by theCursor. If theCursoris -1, it generates anInvalidOperationException. Paste the following code after theMoveNextmethod:object IEnumerator.Current { get { if((Cursor < 0) || (Cursor == intArr.Length)) throw new InvalidOperationException(); return intArr[Cursor]; } }
Using for each to iterate through the custom collection
In Form1.cs, on the Design tab, drag a button to the form.
Double-click the button and add the following sample code to the
Clickevent of the button:CustomCollection MyCol = new CustomCollection(); foreach (object MyObj in MyCol) MessageBox.Show(MyObj.ToString());Press F5 to run the application, and then click the button.
Note
A message box displays the items in the custom collection.
How does this work? For each calls the GetEnumerator method to create the Enumerator object and calls the MoveNext method to set the Cursor to the first item. Then the current property is accessed to get the item in MyObj. This is repeated until MoveNext returns False.