IOrderedDictionary Antarmuka
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mewakili kumpulan pasangan kunci/nilai yang diindeks.
public interface class IOrderedDictionary : System::Collections::IDictionary
public interface IOrderedDictionary : System.Collections.IDictionary
type IOrderedDictionary = interface
interface ICollection
interface IEnumerable
interface IDictionary
type IOrderedDictionary = interface
interface IDictionary
interface ICollection
interface IEnumerable
Public Interface IOrderedDictionary
Implements IDictionary
- Turunan
- Penerapan
Contoh
Contoh kode berikut menunjukkan implementasi sederhana IOrderedDictionary berdasarkan ArrayList kelas . Yang diimplementasikan IOrderedDictionary menyimpan nama depan sebagai kunci dan nama belakang sebagai nilai, dengan persyaratan tambahan bahwa setiap nama depan unik.
using System;
using System.Collections;
using System.Collections.Specialized;
public class People : IOrderedDictionary
{
private ArrayList _people;
public People(int numItems)
{
_people = new ArrayList(numItems);
}
public int IndexOfKey(object key)
{
for (int i = 0; i < _people.Count; i++)
{
if (((DictionaryEntry)_people[i]).Key == key)
return i;
}
// key not found, return -1.
return -1;
}
public object this[object key]
{
get
{
return ((DictionaryEntry)_people[IndexOfKey(key)]).Value;
}
set
{
_people[IndexOfKey(key)] = new DictionaryEntry(key, value);
}
}
// IOrderedDictionary Members
public IDictionaryEnumerator GetEnumerator()
{
return new PeopleEnum(_people);
}
public void Insert(int index, object key, object value)
{
if (IndexOfKey(key) != -1)
{
throw new ArgumentException("An element with the same key already exists in the collection.");
}
_people.Insert(index, new DictionaryEntry(key, value));
}
public void RemoveAt(int index)
{
_people.RemoveAt(index);
}
public object this[int index]
{
get
{
return ((DictionaryEntry)_people[index]).Value;
}
set
{
object key = ((DictionaryEntry)_people[index]).Key;
_people[index] = new DictionaryEntry(key, value);
}
}
// IDictionary Members
public void Add(object key, object value)
{
if (IndexOfKey(key) != -1)
{
throw new ArgumentException("An element with the same key already exists in the collection.");
}
_people.Add(new DictionaryEntry(key, value));
}
public void Clear()
{
_people.Clear();
}
public bool Contains(object key)
{
if (IndexOfKey(key) == -1)
{
return false;
}
else
{
return true;
}
}
public bool IsFixedSize
{
get
{
return false;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public ICollection Keys
{
get
{
ArrayList KeyCollection = new ArrayList(_people.Count);
for (int i = 0; i < _people.Count; i++)
{
KeyCollection.Add( ((DictionaryEntry)_people[i]).Key );
}
return KeyCollection;
}
}
public void Remove(object key)
{
_people.RemoveAt(IndexOfKey(key));
}
public ICollection Values
{
get
{
ArrayList ValueCollection = new ArrayList(_people.Count);
for (int i = 0; i < _people.Count; i++)
{
ValueCollection.Add( ((DictionaryEntry)_people[i]).Value );
}
return ValueCollection;
}
}
// ICollection Members
public void CopyTo(Array array, int index)
{
_people.CopyTo(array, index);
}
public int Count
{
get
{
return _people.Count;
}
}
public bool IsSynchronized
{
get
{
return _people.IsSynchronized;
}
}
public object SyncRoot
{
get
{
return _people.SyncRoot;
}
}
// IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return new PeopleEnum(_people);
}
}
public class PeopleEnum : IDictionaryEnumerator
{
public ArrayList _people;
// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
public PeopleEnum(ArrayList list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Count);
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
public DictionaryEntry Entry
{
get
{
return (DictionaryEntry)Current;
}
}
public object Key
{
get
{
try
{
return ((DictionaryEntry)_people[position]).Key;
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
public object Value
{
get
{
try
{
return ((DictionaryEntry)_people[position]).Value;
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
class App
{
static void Main()
{
People peopleCollection = new People(3);
peopleCollection.Add("John", "Smith");
peopleCollection.Add("Jim", "Johnson");
peopleCollection.Add("Sue", "Rabon");
Console.WriteLine("Displaying the entries in peopleCollection:");
foreach (DictionaryEntry de in peopleCollection)
{
Console.WriteLine("{0} {1}", de.Key, de.Value);
}
Console.WriteLine();
Console.WriteLine("Displaying the entries in the modified peopleCollection:");
peopleCollection["Jim"] = "Jackson";
peopleCollection.Remove("Sue");
peopleCollection.Insert(0, "Fred", "Anderson");
foreach (DictionaryEntry de in peopleCollection)
{
Console.WriteLine("{0} {1}", de.Key, de.Value);
}
}
}
/* This code produces output similar to the following:
*
* Displaying the entries in peopleCollection:
* John Smith
* Jim Johnson
* Sue Rabon
*
* Displaying the entries in the modified peopleCollection:
* Fred Anderson
* John Smith
* Jim Jackson
*/
Imports System.Collections
Imports System.Collections.Specialized
Public Class People
Implements IOrderedDictionary
Private _people As ArrayList
Public Sub New(ByVal numItems As Integer)
_people = New ArrayList(numItems)
End Sub
Public Function IndexOfKey(ByVal key As Object) As Integer
Dim i As Integer
For i = 0 To _people.Count - 1
If CType(_people(i), DictionaryEntry).Key = key Then
Return i
End If
Next i
' key not found, return -1.
Return -1
End Function
' IOrderedDictionary Members
Public Function GetEnumerator() As IDictionaryEnumerator _
Implements IOrderedDictionary.GetEnumerator
Return New PeopleEnum(_people)
End Function
Public Sub Insert(ByVal index As Integer, ByVal key As Object, _
ByVal value As Object) Implements IOrderedDictionary.Insert
If Not IndexOfKey(key) = -1 Then
Throw New ArgumentException("An element with the same key already exists in the collection.")
End If
_people.Insert(index, New DictionaryEntry(key, value))
End Sub
Public Sub RemoveAt(ByVal index As Integer) _
Implements IOrderedDictionary.RemoveAt
_people.RemoveAt(index)
End Sub
Public Property Item(ByVal index As Integer) As Object _
Implements IOrderedDictionary.Item
Get
Return CType(_people(index), DictionaryEntry).Value
End Get
Set(ByVal value As Object)
Dim key As Object = CType(_people(index), DictionaryEntry).Key
_people(index) = New DictionaryEntry(key, value)
End Set
End Property
' IDictionary Members
Public Function IDictionaryGetEnumerator() As IDictionaryEnumerator _
Implements IDictionary.GetEnumerator
Return New PeopleEnum(_people)
End Function
Public Property Item(ByVal key As Object) As Object _
Implements IDictionary.Item
Get
Return CType(_people(IndexOfKey(key)), DictionaryEntry).Value
End Get
Set(ByVal value)
_people(IndexOfKey(key)) = New DictionaryEntry(key, value)
End Set
End Property
Public Sub Add(ByVal key As Object, ByVal value As Object) _
Implements IDictionary.Add
If Not IndexOfKey(key) = -1 Then
Throw New ArgumentException("An element with the same key already exists in the collection.")
End If
_people.Add(New DictionaryEntry(key, value))
End Sub
Public Sub Clear() Implements IDictionary.Clear
_people.Clear()
End Sub
Public Function Contains(ByVal key As Object) As Boolean _
Implements IDictionary.Contains
If IndexOfKey(key) = -1 Then
Return False
Else
Return True
End If
End Function
Public ReadOnly Property IsFixedSize() As Boolean _
Implements IDictionary.IsFixedSize
Get
Return False
End Get
End Property
Public ReadOnly Property IsReadOnly() As Boolean _
Implements IDictionary.IsReadOnly
Get
Return False
End Get
End Property
Public ReadOnly Property Keys() As ICollection _
Implements IDictionary.Keys
Get
Dim KeyCollection As ArrayList = New ArrayList(_people.Count)
Dim i As Integer
For i = 0 To _people.Count - 1
KeyCollection.Add( CType(_people(i), DictionaryEntry).Key )
Next i
Return KeyCollection
End Get
End Property
Public Sub Remove(ByVal key As Object) _
Implements IDictionary.Remove
_people.RemoveAt(IndexOfKey(key))
End Sub
Public ReadOnly Property Values() As ICollection _
Implements IDictionary.Values
Get
Dim ValueCollection As ArrayList = New ArrayList(_people.Count)
Dim i As Integer
For i = 0 To _people.Count - 1
ValueCollection.Add( CType(_people(i), DictionaryEntry).Value )
Next i
Return ValueCollection
End Get
End Property
' ICollection Members
Public Sub CopyTo(ByVal array As Array, ByVal index As Integer) _
Implements ICollection.CopyTo
_people.CopyTo(Array, index)
End Sub
Public ReadOnly Property Count() As Integer _
Implements ICollection.Count
Get
Return _people.Count
End Get
End Property
Public ReadOnly Property IsSynchronized() As Boolean _
Implements ICollection.IsSynchronized
Get
Return _people.IsSynchronized
End Get
End Property
Public ReadOnly Property SyncRoot() As Object _
Implements ICollection.SyncRoot
Get
Return _people.SyncRoot
End Get
End Property
' IEnumerable Members
Public Function IEnumerableGetEnumerator() As IEnumerator _
Implements IEnumerable.GetEnumerator
Return New PeopleEnum(_people)
End Function
End Class
Public Class PeopleEnum
Implements IDictionaryEnumerator
Public _people As ArrayList
' Enumerators are positioned before the first element
' until the first MoveNext() call.
Dim position As Integer = -1
Public Sub New(ByVal list As ArrayList)
_people = list
End Sub
Public Function MoveNext() As Boolean _
Implements IEnumerator.MoveNext
position = position + 1
Return (position < _people.Count)
End Function
Public Sub Reset() _
Implements IEnumerator.Reset
position = -1
End Sub
Public ReadOnly Property Current() As Object _
Implements IEnumerator.Current
Get
Try
Return _people(position)
Catch e As IndexOutOfRangeException
Throw New InvalidOperationException()
End Try
End Get
End Property
Public ReadOnly Property Entry() As DictionaryEntry _
Implements IDictionaryEnumerator.Entry
Get
Return CType(Current, DictionaryEntry)
End Get
End Property
Public ReadOnly Property Key() As Object _
Implements IDictionaryEnumerator.Key
Get
Try
Return CType(_people(position), DictionaryEntry).Key
Catch e As IndexOutOfRangeException
Throw New InvalidOperationException()
End Try
End Get
End Property
Public ReadOnly Property Value() As Object _
Implements IDictionaryEnumerator.Value
Get
Try
Return CType(_people(position), DictionaryEntry).Value
Catch e As IndexOutOfRangeException
Throw New InvalidOperationException()
End Try
End Get
End Property
End Class
Class App
Shared Sub Main()
Dim peopleCollection As People = New People(3)
peopleCollection.Add("John", "Smith")
peopleCollection.Add("Jim", "Johnson")
peopleCollection.Add("Sue", "Rabon")
Console.WriteLine("Displaying the entries in peopleCollection:")
Dim de As DictionaryEntry
For Each de In peopleCollection
Console.WriteLine("{0} {1}", de.Key, de.Value)
Next
Console.WriteLine()
Console.WriteLine("Displaying the entries in the modified peopleCollection:")
'peopleCollection("Jim") = "Jackson"
peopleCollection.Remove("Sue")
peopleCollection.Insert(0, "Fred", "Anderson")
For Each de In peopleCollection
Console.WriteLine("{0} {1}", de.Key, de.Value)
Next
End Sub
End Class
' This code produces output similar to the following:
'
' Displaying the entries in peopleCollection:
' John Smith
' Jim Johnson
' Sue Rabon
'
' Displaying the entries in the modified peopleCollection:
' Fred Anderson
' John Smith
' Jim Jackson
Keterangan
IOrderedDictionary elemen dapat diakses baik dengan kunci atau dengan indeks.
Setiap elemen adalah pasangan kunci/nilai yang DictionaryEntry disimpan dalam struktur.
Setiap pasangan harus memiliki kunci unik yang bukan null, tetapi nilainya bisa null dan tidak harus unik. Antarmuka IOrderedDictionary memungkinkan kunci dan nilai yang terkandung untuk dijumlahkan, tetapi tidak menyiratkan urutan pengurutan tertentu.
Pernyataan foreach bahasa C# (For Each dalam Visual Basic) mengembalikan objek dari jenis elemen dalam koleksi. Karena setiap elemen IDictionary adalah pasangan kunci/nilai, jenis elemen bukan jenis kunci atau jenis nilai. Sebaliknya, jenis elemennya adalah DictionaryEntry, seperti yang ditunjukkan contoh berikut.
foreach (DictionaryEntry de in myOrderedDictionary)
{
//...
}
For Each de In myOrderedDictionary
'...
Next
Pernyataan itu foreach adalah pembungkus di sekitar enumerator, yang hanya memungkinkan membaca dari, tidak menulis ke, koleksi.
Catatan Bagi Implementer
Kelas penerapan harus memiliki sarana untuk membandingkan kunci.
Properti
| Nama | Deskripsi |
|---|---|
| Count |
Mendapatkan jumlah elemen yang terkandung dalam ICollection. (Diperoleh dari ICollection) |
| IsFixedSize |
Mendapatkan nilai yang menunjukkan apakah IDictionary objek memiliki ukuran tetap. (Diperoleh dari IDictionary) |
| IsReadOnly |
Mendapatkan nilai yang menunjukkan apakah objek IDictionary bersifat baca-saja. (Diperoleh dari IDictionary) |
| IsSynchronized |
Mendapatkan nilai yang menunjukkan apakah akses ke ICollection disinkronkan (utas aman). (Diperoleh dari ICollection) |
| Item[Int32] |
Mendapatkan atau mengatur elemen pada indeks yang ditentukan. |
| Item[Object] |
Mendapatkan atau mengatur elemen dengan kunci yang ditentukan. (Diperoleh dari IDictionary) |
| Keys |
Mendapatkan objek yang ICollection berisi kunci IDictionary objek. (Diperoleh dari IDictionary) |
| SyncRoot |
Mendapatkan objek yang dapat digunakan untuk menyinkronkan akses ke ICollection. (Diperoleh dari ICollection) |
| Values |
Mendapatkan objek yang ICollection berisi nilai dalam IDictionary objek. (Diperoleh dari IDictionary) |
Metode
| Nama | Deskripsi |
|---|---|
| Add(Object, Object) |
Menambahkan elemen dengan kunci dan nilai yang disediakan ke IDictionary objek. (Diperoleh dari IDictionary) |
| Clear() |
Menghapus semua elemen dari IDictionary objek. (Diperoleh dari IDictionary) |
| Contains(Object) |
Menentukan apakah IDictionary objek berisi elemen dengan kunci yang ditentukan. (Diperoleh dari IDictionary) |
| CopyTo(Array, Int32) |
Menyalin elemen ICollection ke Array, dimulai pada indeks tertentu Array . (Diperoleh dari ICollection) |
| GetEnumerator() |
Mengembalikan enumerator yang berulang melalui koleksi IOrderedDictionary. |
| Insert(Int32, Object, Object) |
Menyisipkan pasangan kunci/nilai ke dalam koleksi pada indeks yang ditentukan. |
| Remove(Object) |
Menghapus elemen dengan kunci yang ditentukan dari IDictionary objek. (Diperoleh dari IDictionary) |
| RemoveAt(Int32) |
Menghapus elemen pada indeks yang ditentukan. |
Metode Ekstensi
| Nama | Deskripsi |
|---|---|
| AsParallel(IEnumerable) |
Mengaktifkan paralelisasi kueri. |
| AsQueryable(IEnumerable) |
Mengonversi IEnumerable menjadi IQueryable. |
| Cast<TResult>(IEnumerable) |
Melemparkan elemen IEnumerable ke jenis yang ditentukan. |
| OfType<TResult>(IEnumerable) |
Memfilter elemen IEnumerable berdasarkan jenis tertentu. |