ReadOnlyCollectionBase.Count Property
.NET Framework 2.0
Gets the number of elements contained in the ReadOnlyCollectionBase instance.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
The following code example implements the ReadOnlyCollectionBase class.
using namespace System; using namespace System::Collections; public ref class ROCollection: public ReadOnlyCollectionBase { public: ROCollection( IList^ sourceList ) { InnerList->AddRange( sourceList ); } property Object^ Item [int] { Object^ get( int index ) { return (InnerList[ index ]); } } int IndexOf( Object^ value ) { return (InnerList->IndexOf( value )); } bool Contains( Object^ value ) { return (InnerList->Contains( value )); } }; void PrintIndexAndValues( ROCollection^ myCol ); void PrintValues2( ROCollection^ myCol ); int main() { // Create an ArrayList. ArrayList^ myAL = gcnew ArrayList; myAL->Add( "red" ); myAL->Add( "blue" ); myAL->Add( "yellow" ); myAL->Add( "green" ); myAL->Add( "orange" ); myAL->Add( "purple" ); // Create a new ROCollection that contains the elements in myAL. ROCollection^ myCol = gcnew ROCollection( myAL ); // Display the contents of the collection using the enumerator. Console::WriteLine( "Contents of the collection (using enumerator):" ); PrintValues2( myCol ); // Display the contents of the collection using the Count property and the Item property. Console::WriteLine( "Contents of the collection (using Count and Item):" ); PrintIndexAndValues( myCol ); // Search the collection with Contains and IndexOf. Console::WriteLine( "Contains yellow: {0}", myCol->Contains( "yellow" ) ); Console::WriteLine( "orange is at index {0}.", myCol->IndexOf( "orange" ) ); Console::WriteLine(); } // Uses the Count property and the Item property. void PrintIndexAndValues( ROCollection^ myCol ) { for ( int i = 0; i < myCol->Count; i++ ) Console::WriteLine( " [{0}]: {1}", i, myCol->Item[ i ] ); Console::WriteLine(); } // Uses the enumerator. void PrintValues2( ROCollection^ 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): red blue yellow green orange purple Contents of the collection (using Count and Item): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple Contains yellow: True orange is at index 4. */
import System.*;
import System.Collections.*;
public class ROCollection extends ReadOnlyCollectionBase
{
public ROCollection(IList sourceList)
{
get_InnerList().AddRange(sourceList);
} //ROCollection
/** @property
*/
public Object get_Item(int index)
{
return get_InnerList().get_Item(index);
} //get_Item
public int IndexOf(Object value)
{
return get_InnerList().IndexOf(value);
} //IndexOf
public boolean Contains(Object value)
{
return get_InnerList().Contains(value);
} //Contains
} //ROCollection
public class SamplesCollectionBase
{
public static void main(String[] args)
{
// Create an ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("red");
myAL.Add("blue");
myAL.Add("yellow");
myAL.Add("green");
myAL.Add("orange");
myAL.Add("purple");
// Create a new ROCollection that contains the elements in myAL.
ROCollection myCol = new ROCollection(myAL);
// Display the contents of the collection using for. This is the
// preferred method.
Console.WriteLine("Contents of the collection (using for):");
PrintValues1(myCol);
// Display the contents of the collection using the enumerator.
Console.WriteLine("Contents of the collection (using enumerator):");
PrintValues2(myCol);
// Display the contents of the collection using the Count property and
// the Item property.
Console.WriteLine("Contents of the collection (using Count and Item):");
PrintIndexAndValues(myCol);
// Search the collection with Contains and IndexOf.
Console.WriteLine("Contains yellow: {0}",
(System.Boolean)myCol.Contains("yellow"));
Console.WriteLine("orange is at index {0}.",
(Int32)myCol.IndexOf("orange"));
Console.WriteLine();
} //main
// Uses the Count property and the Item property.
public static void PrintIndexAndValues(ROCollection 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(ROCollection myCol)
{
for (int iCtr = 0; iCtr < myCol.get_Count(); iCtr++ ) {
Object obj = myCol.get_Item(iCtr);
Console.WriteLine(" {0}", obj);
}
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(ROCollection myCol)
{
System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
while(myEnumerator.MoveNext()) {
Console.WriteLine(" {0}", myEnumerator.get_Current());
}
Console.WriteLine();
} //PrintValues2
} //SamplesCollectionBase
/*
This code produces the following output.
Contents of the collection (using for):
red
blue
yellow
green
orange
purple
Contents of the collection (using enumerator):
red
blue
yellow
green
orange
purple
Contents of the collection (using Count and Item):
[0]: red
[1]: blue
[2]: yellow
[3]: green
[4]: orange
[5]: purple
Contains yellow: True
orange is at index 4.
*/
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, 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: