Provides the abstract base class for a strongly typed collection.
public abstract class CollectionBase: Object, IList, ICollection, IEnumerable
ObjectCollectionBase
// This examples shows how to implement a CollectionBase.
// It shows also some properties and method of the CollectionBase class.
using System;
using System.Collections;
using System.Diagnostics;
public class SampleCollectionBase
{
static void Main()
{
// Creates and initializes a new CollectionBase.
MyCharCollection myCharCol = new MyCharCollection();
// Adds elements to the collection.
myCharCol.Add( 'a' );
myCharCol.Add( 'b' );
myCharCol.Add( 'c' );
myCharCol.Add( 'd' );
myCharCol.Add( 'e' );
// Checks some properties of the CollectionBase instance.
Debug.WriteLine("Properties of the collection:");
bool isfixedS = myCharCol.IsFixedSize;
Debug.WriteLine("\tFixed size:" +isfixedS
.ToString());
bool isSync = myCharCol.IsSynchronised;
Debug.WriteLine("\tSynchronised:" +isSync.ToString());
// Displays the contents of the collection.
Debug.WriteLine("\nInitial contents of the collection:");
DisplayValues(myCharCol);
// Searches the collection with Contains and IndexOf.
bool contains3 = myCharCol.Contains( 'c' );
Debug.WriteLine("\t"+@"Contains ""c"":"+contains3);
int i2 =myCharCol.IndexOf( 'd' );
Debug.WriteLine("\t"+@"Index of ""d"":"+i2.ToString() +"\n");
// Inserts an element into the collection at index 3.
myCharCol.Insert( 3, 'A');
Debug.WriteLine("Contents of the collection after inserting at index 3:");
DisplayValues(myCharCol);
// Gets and sets an element using the index.
Debug.WriteLine("\tElements at index 2:" +myCharCol[2]);
myCharCol[4] ='C';
Debug.WriteLine("\n"+@"Contents of the collection after setting the element at index 4 to ""C"":");
DisplayValues2(myCharCol);
// Removes an element from the collection.
myCharCol.Remove('a' );
Debug.WriteLine("\n"+@"Contents of the collection after deleting ""a"":");
DisplayValues2(myCharCol);
// Creates and initializes a new array of char.
Array myCharArray = new Char[7] {'t','u','v','w','x','y','z'};
// Displays the content of an array of char.
Debug.Write("\nContents of the array of string:");
DisplayArrayValues(myCharArray);
// Copies the elements of the SortedList into the array.
myCharCol.CopyTo((Char[])myCharArray, 0);
// Display of the content of the array after copying the elements of the SortedList in it.
Debug.Write("\nContents of the array after copying the elements of the SortedList in it:");
DisplayArrayValues(myCharArray);
}
// Displays values using Count and this[].
static void DisplayValues( MyCharCollection myCharColBase )
{
int i=0;
int count = myCharColBase.Count;
for(int y=1; y<=count;y++)
{
Debug.WriteLine("\t["+(i++)+"]" +myCharColBase[y-1]);
}
}
// Displays values using an enumerator.
static void DisplayValues2( CollectionBase myCharColBase)
{
int i=0;
System.Collections.IEnumerator myEnumerator = myCharColBase.GetEnumerator();
while ( myEnumerator.MoveNext() )
Debug.WriteLine("\t["+(i++)+"]" + myEnumerator.Current);
}
// Displays values of an array.
public static void DisplayArrayValues ( Array array )
{
for ( int i = 0; i < array.Length; i++ )
{
Debug.Write(array.GetValue(i)+" ");
}
}
}
public class MyCharCollection : CollectionBase
{
public Char this[ int index ]
{
get
{
return( (Char) List[index] );
}
set
{
List[index] = value;
}
}
public int Add( Char value )
{
return( List.Add( value ) );
}
public int IndexOf( Char value )
{
return( List.IndexOf( value ) );
}
public void Insert( int index, Char value )
{
List.Insert( index, value );
}
public void Remove( Char value )
{
List.Remove( value );
}
public void CopyTo(Array array, int index )
{
InnerList.CopyTo(array, index );
}
public bool Contains( Char value )
{
return( List.Contains( value ) );
}
public bool IsFixedSize
{
get
{
return( List.IsFixedSize );
}
}
public bool IsSynchronised
{
get
{
return( List.IsSynchronized );
}
}
protected override void OnInsert( int index, Object value )
{
// Insert additional code to be executed only when inserting values.
}
protected override void OnRemove( int index, Object value )
{
// Insert additional code to be executed only when removing values.
}
protected override void OnSet( int index, Object oldValue, Object newValue )
{
// Insert additional code to be executed only when setting values.
}
}
/*
The output of the sample.
Properties of the collection:
Fixed size:False
Synchronised:False
Initial contents of the collection:
[0]a
[1]b
[2]c
[3]d
[4]e
Contains "c":True
Index of "d":3
Contents of the collection after inserting at index 3:
[0]a
[1]b
[2]c
[3]A
[4]d
[5]e
Elements at index 2:c
Contents of the collection after setting the element at index 4 to "C":
[0]a
[1]b
[2]c
[3]A
[4]C
[5]e
Contents of the collection after deleting "a":
[0]b
[1]c
[2]A
[3]C
[4]e
Contents of the array of string:t u v w x y z
Contents of the array after copying the elements of the SortedList in it:b c A C e y z
*/
Namespace System.Collections Flash Library corlib.scl Flash Library Version 2.0.0.2466 Silverlight Library System.Windows.Forms.dll
| © 2003-2007 NETiKA Technologies. All rights reserved. |