ReadOnlyCollectionBase Klass
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Tillhandahåller basklassen abstract för en starkt skriven icke-generisk skrivskyddad samling.
public ref class ReadOnlyCollectionBase abstract : System::Collections::ICollection
public abstract class ReadOnlyCollectionBase : System.Collections.ICollection
[System.Serializable]
public abstract class ReadOnlyCollectionBase : System.Collections.ICollection
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class ReadOnlyCollectionBase : System.Collections.ICollection
type ReadOnlyCollectionBase = class
interface ICollection
interface IEnumerable
[<System.Serializable>]
type ReadOnlyCollectionBase = class
interface ICollection
interface IEnumerable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ReadOnlyCollectionBase = class
interface ICollection
interface IEnumerable
Public MustInherit Class ReadOnlyCollectionBase
Implements ICollection
- Arv
-
ReadOnlyCollectionBase
- Härledda
- Attribut
- Implementeringar
Exempel
I följande kodexempel implementeras ReadOnlyCollectionBase klassen.
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 foreach. This is the preferred method.
Console.WriteLine( "Contents of the collection (using foreach):" );
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}", myCol.Contains( "yellow" ) );
Console.WriteLine( "orange is at index {0}.", myCol.IndexOf( "orange" ) );
Console.WriteLine();
}
// Uses the Count property and the Item property.
public static void PrintIndexAndValues( ROCollection myCol ) {
for ( int i = 0; i < myCol.Count; i++ )
Console.WriteLine( " [{0}]: {1}", i, myCol[i] );
Console.WriteLine();
}
// Uses the foreach statement which hides the complexity of the enumerator.
// NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
public static void PrintValues1( ROCollection myCol ) {
foreach ( Object obj in myCol )
Console.WriteLine( " {0}", obj );
Console.WriteLine();
}
// Uses the enumerator.
// NOTE: The foreach 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.Current );
Console.WriteLine();
}
}
/*
This code produces the following output.
Contents of the collection (using foreach):
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.
*/
Imports System.Collections
Public Class ROCollection
Inherits ReadOnlyCollectionBase
Public Sub New(sourceList As IList)
InnerList.AddRange(sourceList)
End Sub
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
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 For Each. This is the preferred method.
Console.WriteLine("Contents of the collection (using For Each):")
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}", myCol.Contains("yellow"))
Console.WriteLine("orange is at index {0}.", myCol.IndexOf("orange"))
Console.WriteLine()
End Sub
' Uses the Count property and the Item property.
Public Shared Sub PrintIndexAndValues(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
' Uses the For Each statement which hides the complexity of the enumerator.
' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintValues1(myCol As ROCollection)
Dim obj As [Object]
For Each obj In myCol
Console.WriteLine(" {0}", obj)
Next obj
Console.WriteLine()
End Sub
' Uses the enumerator.
' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
Public Shared Sub PrintValues2(myCol As ROCollection)
Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(" {0}", myEnumerator.Current)
End While
Console.WriteLine()
End Sub
End Class
'This code produces the following output.
'
'Contents of the collection (using For Each):
' 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.
Kommentarer
En ReadOnlyCollectionBase instans är alltid skrivskyddad. Se CollectionBase för en ändringsbar version av den här klassen.
Important
Vi rekommenderar inte att du använder ReadOnlyCollectionBase klassen för ny utveckling. I stället rekommenderar vi att du använder den allmänna ReadOnlyCollection<T> klassen. Mer information finns i Non-generic samlingar bör inte användas på GitHub.
Anteckningar till implementerare
Den här basklassen tillhandahålls för att göra det enklare för implementerare att skapa en starkt skrivskyddad anpassad samling. Implementerare uppmuntras att utöka den här basklassen i stället för att skapa sin egen. Medlemmar i den här basklassen är skyddade och är avsedda att endast användas via en härledd klass.
Den här klassen gör den underliggande samlingen tillgänglig via InnerList egenskapen, som endast är avsedd att användas av klasser som härleds direkt från ReadOnlyCollectionBase. Den härledda klassen måste se till att dess egna användare inte kan ändra den underliggande samlingen.
Konstruktorer
| Name | Description |
|---|---|
| ReadOnlyCollectionBase() |
Initierar en ny instans av ReadOnlyCollectionBase klassen. |
Egenskaper
| Name | Description |
|---|---|
| Count |
Hämtar antalet element som finns i instansen ReadOnlyCollectionBase . |
| InnerList |
Hämtar listan över element som finns i instansen ReadOnlyCollectionBase . |
Metoder
| Name | Description |
|---|---|
| Equals(Object) |
Avgör om det angivna objektet är lika med det aktuella objektet. (Ärvd från Object) |
| GetEnumerator() |
Returnerar en uppräkning som itererar genom instansen ReadOnlyCollectionBase . |
| GetHashCode() |
Fungerar som standard-hash-funktion. (Ärvd från Object) |
| GetType() |
Hämtar den aktuella instansen Type . (Ärvd från Object) |
| MemberwiseClone() |
Skapar en ytlig kopia av den aktuella Object. (Ärvd från Object) |
| ToString() |
Returnerar en sträng som representerar det aktuella objektet. (Ärvd från Object) |
Explicita gränssnittsimplementeringar
| Name | Description |
|---|---|
| ICollection.CopyTo(Array, Int32) |
Kopierar hela ReadOnlyCollectionBase till en kompatibel endimensionell Array, med början vid det angivna indexet för målmatrisen. |
| ICollection.IsSynchronized |
Hämtar ett värde som anger om åtkomsten till ett ReadOnlyCollectionBase objekt synkroniseras (trådsäker). |
| ICollection.SyncRoot |
Hämtar ett objekt som kan användas för att synkronisera åtkomst till ett ReadOnlyCollectionBase objekt. |
Tilläggsmetoder
| Name | Description |
|---|---|
| AsParallel(IEnumerable) |
Möjliggör parallellisering av en fråga. |
| AsQueryable(IEnumerable) |
Konverterar en IEnumerable till en IQueryable. |
| Cast<TResult>(IEnumerable) |
Omvandlar elementen i en IEnumerable till den angivna typen. |
| OfType<TResult>(IEnumerable) |
Filtrerar elementen i en IEnumerable baserat på en angiven typ. |
Gäller för
Trådsäkerhet
Offentliga statiska (Shared i Visual Basic) medlemmar av den här typen är trådsäkra. Vilka som helst instansmedlemmar garanteras inte att vara trådsäkra.
Den här implementeringen tillhandahåller ingen synkroniserad (trådsäker) omslutning för en ReadOnlyCollectionBase, men härledda klasser kan skapa sina egna synkroniserade versioner av ReadOnlyCollectionBase egenskapen SyncRoot .
Att räkna upp genom en samling är i sig inte en trådsäker procedur. Även när en samling synkroniseras kan andra trådar fortfarande ändra samlingen, vilket gör att uppräknaren genererar ett undantag. För att garantera trådsäkerheten under uppräkningen kan du antingen låsa samlingen under hela uppräkningen eller fånga undantagen från ändringar som gjorts av andra trådar.