This documentation is archived and is not being maintained.
ReadOnlyCollectionBase.Count Property
.NET Framework 1.1
Gets the number of elements contained in the ReadOnlyCollectionBase instance.
[Visual Basic] Public Overridable ReadOnly Property Count As Integer Implements _ ICollection.Count [C#] public virtual int Count {get;} [C++] public: __property virtual int get_Count(); [JScript] public function get Count() : int;
Property Value
The number of elements contained in the ReadOnlyCollectionBase instance.
Implements
Example
[Visual Basic, C#, C++] The following code example implements the ReadOnlyCollectionBase class.
[Visual Basic] Imports System Imports System.Collections Public Class ROCollection Inherits ReadOnlyCollectionBase Public Sub New(sourceList As IList) InnerList.AddRange(sourceList) End Sub 'New Default Public ReadOnly Property Item(index As Integer) As [Object] Get Return InnerList(index) End Get End Property Public Function IndexOf(value As [Object]) As Integer Return InnerList.IndexOf(value) End Function 'IndexOf Public Function Contains(value As [Object]) As Boolean Return InnerList.Contains(value) End Function 'Contains End Class 'ROCollection Public Class SamplesCollectionBase Public Shared Sub Main() ' Create an ArrayList. Dim myAL As 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. Dim myCol As New ROCollection(myAL) ' Display the contents of the collection using the enumerator. Console.WriteLine("Contents of the collection (using enumerator):") 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() ' Display the contents of the collection using the Count property and the Item property. Console.WriteLine("Contents of the collection (using Count and Item):") PrintIndexAndValues2(myCol) End Sub 'Main Public Shared Sub PrintIndexAndValues(myCol As ROCollection) Dim i As Integer = 0 Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator() While myEnumerator.MoveNext() Console.WriteLine(" [{0}]: {1}", i, myEnumerator.Current) i += 1 End While Console.WriteLine() End Sub 'PrintIndexAndValues Public Shared Sub PrintIndexAndValues2(myCol As ROCollection) Dim i As Integer For i = 0 To myCol.Count - 1 Console.WriteLine(" [{0}]: {1}", i, myCol(i)) Next i Console.WriteLine() End Sub 'PrintIndexAndValues2 End Class 'SamplesCollectionBase 'This code produces the following output. ' 'Contents of the collection (using enumerator): ' [0]: red ' [1]: blue ' [2]: yellow ' [3]: green ' [4]: orange ' [5]: purple ' 'Contains yellow: True 'orange is at index 4. ' 'Contents of the collection (using Count and Item): ' [0]: red ' [1]: blue ' [2]: yellow ' [3]: green ' [4]: orange ' [5]: purple [C#] using System; using System.Collections; public class ROCollection : ReadOnlyCollectionBase { public ROCollection( IList sourceList ) { InnerList.AddRange( sourceList ); } public Object this[ int index ] { get { return( InnerList[index] ); } } public int IndexOf( Object value ) { return( InnerList.IndexOf( value ) ); } public bool Contains( Object value ) { return( InnerList.Contains( value ) ); } } public class SamplesCollectionBase { public static void Main() { // 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 the enumerator. Console.WriteLine( "Contents of the collection (using enumerator):" ); 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(); // Display the contents of the collection using the Count property and the Item property. Console.WriteLine( "Contents of the collection (using Count and Item):" ); PrintIndexAndValues2( myCol ); } public static void PrintIndexAndValues( ROCollection myCol ) { int i = 0; System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator(); while ( myEnumerator.MoveNext() ) Console.WriteLine( " [{0}]: {1}", i++, myEnumerator.Current ); Console.WriteLine(); } public static void PrintIndexAndValues2( ROCollection myCol ) { for ( int i = 0; i < myCol.Count; i++ ) Console.WriteLine( " [{0}]: {1}", i, myCol[i] ); Console.WriteLine(); } } /* This code produces the following output. Contents of the collection (using enumerator): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple Contains yellow: True orange is at index 4. Contents of the collection (using Count and Item): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple */ [C++] #using <mscorlib.dll> using namespace System; using namespace System::Collections; public __gc class ROCollection : public ReadOnlyCollectionBase { public: ROCollection( IList* sourceList ) { InnerList->AddRange( sourceList ); } public: __property Object* get_Item( int index ) { return( InnerList->Item[index] ); } public: int IndexOf( Object* value ) { return( InnerList->IndexOf( value ) ); } public: bool Contains( Object* value ) { return( InnerList->Contains( value ) ); } }; static void PrintIndexAndValues( ROCollection* myCol ) { int i = 0; System::Collections::IEnumerator* myEnumerator = myCol->GetEnumerator(); while ( myEnumerator->MoveNext() ) Console::WriteLine( S" [{0}]: {1}", __box(i++), myEnumerator->Current ); Console::WriteLine(); } static void PrintIndexAndValues2( ROCollection* myCol ) { for ( int i = 0; i < myCol->Count; i++ ) Console::WriteLine( S" [{0}]: {1}", __box(i), myCol->Item[i] ); Console::WriteLine(); } int main() { // Create an ArrayList. ArrayList* myAL = new ArrayList(); myAL->Add( S"red" ); myAL->Add( S"blue" ); myAL->Add( S"yellow" ); myAL->Add( S"green" ); myAL->Add( S"orange" ); myAL->Add( S"purple" ); // Create a new ROCollection that contains the elements in myAL. ROCollection* myCol = new ROCollection( myAL ); // Display the contents of the collection using the enumerator. Console::WriteLine( S"Contents of the collection (using enumerator):" ); PrintIndexAndValues( myCol ); // Search the collection with Contains and IndexOf. Console::WriteLine( S"Contains yellow: {0}", __box(myCol->Contains( S"yellow" ))); Console::WriteLine( S"orange is at index {0}.", __box(myCol->IndexOf( S"orange" ))); Console::WriteLine(); // Display the contents of the collection using the Count property and the Item property. Console::WriteLine( S"Contents of the collection (using Count and Item):" ); PrintIndexAndValues2( myCol ); } /* This code produces the following output. Contents of the collection (using enumerator): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple Contains yellow: True orange is at index 4. Contents of the collection (using Count and Item): [0]: red [1]: blue [2]: yellow [3]: green [4]: orange [5]: purple */
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
ReadOnlyCollectionBase Class | ReadOnlyCollectionBase Members | System.Collections Namespace
Show: