CollectionBase.Count Property
.NET Framework 3.0
Gets the number of elements contained in the CollectionBase instance. This property cannot be overridden.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
The following code example implements the CollectionBase class and uses that implementation to create a collection of Int16 objects.
#using <System.dll> using namespace System; using namespace System::Collections; public ref class Int16Collection: public CollectionBase { public: property Int16 Item [int] { Int16 get( int index ) { return ( (Int16)(List[ index ])); } void set( int index, Int16 value ) { List[ index ] = value; } } int Add( Int16 value ) { return (List->Add( value )); } int IndexOf( Int16 value ) { return (List->IndexOf( value )); } void Insert( int index, Int16 value ) { List->Insert( index, value ); } void Remove( Int16 value ) { List->Remove( value ); } bool Contains( Int16 value ) { // If value is not of type Int16, this will return false. return (List->Contains( value )); } protected: virtual void OnInsert( int /*index*/, Object^ /*value*/ ) override { // Insert additional code to be run only when inserting values. } virtual void OnRemove( int /*index*/, Object^ /*value*/ ) override { // Insert additional code to be run only when removing values. } virtual void OnSet( int /*index*/, Object^ /*oldValue*/, Object^ /*newValue*/ ) override { // Insert additional code to be run only when setting values. } virtual void OnValidate( Object^ value ) override { if ( value->GetType() != Type::GetType( "System.Int16" ) ) throw gcnew ArgumentException( "value must be of type Int16.","value" ); } }; void PrintIndexAndValues( Int16Collection^ myCol ); void PrintValues2( Int16Collection^ myCol ); int main() { // Create and initialize a new CollectionBase. Int16Collection^ myI16 = gcnew Int16Collection; // Add elements to the collection. myI16->Add( (Int16)1 ); myI16->Add( (Int16)2 ); myI16->Add( (Int16)3 ); myI16->Add( (Int16)5 ); myI16->Add( (Int16)7 ); // Display the contents of the collection using the enumerator. Console::WriteLine( "Contents of the collection (using enumerator):" ); PrintValues2( myI16 ); // Display the contents of the collection using the Count property and the Item property. Console::WriteLine( "Initial contents of the collection (using Count and Item):" ); PrintIndexAndValues( myI16 ); // Search the collection with Contains and IndexOf. Console::WriteLine( "Contains 3: {0}", myI16->Contains( 3 ) ); Console::WriteLine( "2 is at index {0}.", myI16->IndexOf( 2 ) ); Console::WriteLine(); // Insert an element into the collection at index 3. myI16->Insert( 3, (Int16)13 ); Console::WriteLine( "Contents of the collection after inserting at index 3:" ); PrintIndexAndValues( myI16 ); // Get and set an element using the index. myI16->Item[ 4 ] = 123; Console::WriteLine( "Contents of the collection after setting the element at index 4 to 123:" ); PrintIndexAndValues( myI16 ); // Remove an element from the collection. myI16->Remove( (Int16)2 ); // Display the contents of the collection using the Count property and the Item property. Console::WriteLine( "Contents of the collection after removing the element 2:" ); PrintIndexAndValues( myI16 ); } // Uses the Count property and the Item property. void PrintIndexAndValues( Int16Collection^ myCol ) { for ( int i = 0; i < myCol->Count; i++ ) Console::WriteLine( " [{0}]: {1}", i, myCol->Item[ i ] ); Console::WriteLine(); } // Uses the enumerator. void PrintValues2( Int16Collection^ myCol ) { System::Collections::IEnumerator^ myEnumerator = myCol->GetEnumerator(); while ( myEnumerator->MoveNext() ) Console::WriteLine( " {0}", myEnumerator->Current ); Console::WriteLine(); } /* This code produces the following output. Contents of the collection (using enumerator): 1 2 3 5 7 Initial contents of the collection (using Count and Item): [0]: 1 [1]: 2 [2]: 3 [3]: 5 [4]: 7 Contains 3: True 2 is at index 1. Contents of the collection after inserting at index 3: [0]: 1 [1]: 2 [2]: 3 [3]: 13 [4]: 5 [5]: 7 Contents of the collection after setting the element at index 4 to 123: [0]: 1 [1]: 2 [2]: 3 [3]: 13 [4]: 123 [5]: 7 Contents of the collection after removing the element 2: [0]: 1 [1]: 3 [2]: 13 [3]: 123 [4]: 7 */
import System.* ;
import System.Collections.*;
import System.Collections.CollectionBase.*;
public class Int16Collection extends CollectionBase
{
/** @property
*/
public short get_Value(int index)
{
short s = System.Convert.ToInt16(get_List().get_Item(index));
return(s) ;
} //get_Value
/** @property
*/
public void set_Value(int index,short value)
{
get_List().set_Item(index, (Int16)value);
} //set_Value
public int Add(short value)
{
return get_List().Add((Int16)value);
} //Add
public int IndexOf(short value)
{
return get_List().IndexOf((Int16)value);
} //IndexOf
public void Insert(int index, short value)
{
get_List().Insert(index, (Int16)value);
} //Insert
public void Remove(short value)
{
get_List().Remove((Int16)value);
} //Remove
public boolean Contains(short value)
{
// If value is not of type Int16, this will return false.
return get_List().Contains((Int16)value);
} //Contains
protected void OnInsert(int index, Object value)
{
// Insert additional code to be run only when inserting values.
} //OnInsert
protected void OnRemove(int index, Object value)
{
// Insert additional code to be run only when removing values.
} //OnRemove
protected void OnSet(int index, Object oldValue, Object newValue)
{
// Insert additional code to be run only when setting values.
} //OnSet
protected void OnValidate(Object value)
{
if ( value.GetType() != Type.GetType("System.Int16") ) {
throw new ArgumentException("value must be of type Int16.",
"value");
}
} //OnValidate
} //Int16Collection
public class SamplesCollectionBase
{
public static void main(String[] args)
{
// Create and initialize a new CollectionBase.
Int16Collection myI16 = new Int16Collection();
// Add elements to the collection.
myI16.Add((Int16)1);
myI16.Add((Int16)2);
myI16.Add((Int16)3);
myI16.Add((Int16)5);
myI16.Add((Int16)7);
// Display the contents of the collection using for.
Console.WriteLine("Contents of the collection (using for):");
PrintValues1(myI16);
// Display the contents of the collection using the enumerator.
Console.WriteLine("Contents of the collection (using enumerator):");
PrintValues2(myI16);
// Display the contents of the collection using the Count property and
// the Item property.
Console.WriteLine("Initial contents of the collection "
+ "(using Count and Item):");
PrintIndexAndValues(myI16);
// Search the collection with Contains and IndexOf.
Console.WriteLine("Contains 3: {0}",
(System.Boolean)myI16.Contains((Int16)3));
Console.WriteLine("2 is at index {0}.",
(Int16)myI16.IndexOf((Int16)2));
Console.WriteLine();
// Insert an element into the collection at index 3.
myI16.Insert(3, (Int16)13);
Console.WriteLine("Contents of the collection after inserting at"
+ " index 3:");
PrintIndexAndValues(myI16);
// Get and set an element using the index.
myI16 .set_Item( 4 ,(Int16)123 );
Console.WriteLine("Contents of the collection after setting the"
+ " element at index 4 to 123:");
PrintIndexAndValues(myI16);
// Remove an element from the collection.
myI16.Remove((Int16)2);
// Display the contents of the collection using the Count property and
// the Item property.
Console.WriteLine("Contents of the collection after removing the"
+ " element 2:");
PrintIndexAndValues(myI16);
} //main
// Uses the Count property and the Item property.
public static void PrintIndexAndValues(Int16Collection myCol)
{
for(int i = 0; i < myCol.get_Count(); i++) {
Console.WriteLine(" [{0}]: {1}", (Int32)i,
myCol.get_Item(i));
}
Console.WriteLine();
} //PrintIndexAndValues
// Uses the for statement which hides the complexity of the enumerator.
// NOTE: The for statement is the preferred way of enumerating the contents
// of a collection.
public static void PrintValues1(Int16Collection myCol)
{
for (int iCtr = 0; iCtr < myCol.get_Count(); iCtr++) {
Console.WriteLine(" {0}", myCol.get_Item(iCtr));
}
Console.WriteLine();
} //PrintValues1
// Uses the enumerator.
// NOTE: The for statement is the preferred way of enumerating the contents
// of a collection.
public static void PrintValues2(Int16Collection myCol)
{
System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
while(myEnumerator.MoveNext()) {
Console.WriteLine(" {0}", myEnumerator.get_Current());
}
Console.WriteLine();
} //PrintValues2
} //SamplesCollectionBase
/*
Contents of the collection (using for):
1
2
3
5
7
Contents of the collection (using enumerator):
1
2
3
5
7
Initial contents of the collection (using Count and Item):
[0]: 1
[1]: 2
[2]: 3
[3]: 5
[4]: 7
Contains 3: True
2 is at index 1.
Contents of the collection after inserting at index 3:
[0]: 1
[1]: 2
[2]: 3
[3]: 13
[4]: 5
[5]: 7
Contents of the collection after setting the element at index 4 to 123:
[0]: 1
[1]: 2
[2]: 3
[3]: 13
[4]: 123
[5]: 7
Contents of the collection after removing the element 2:
[0]: 1
[1]: 3
[2]: 13
[3]: 123
[4]: 7
*/
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: