IOrderedDictionary.GetEnumerator 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回逐一查看 IOrderedDictionary 集合的列舉值。
public:
System::Collections::IDictionaryEnumerator ^ GetEnumerator();
public System.Collections.IDictionaryEnumerator GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.IDictionaryEnumerator
Public Function GetEnumerator () As IDictionaryEnumerator
傳回
整個 IDictionaryEnumerator 集合的 IOrderedDictionary。
實作
範例
下列程式代碼範例會根據 類別示範簡單 IOrderedDictionary 實作 ArrayList 。 實作 IOrderedDictionary 的 會將名字儲存為索引鍵,並將姓氏儲存為值,並新增每個名字是唯一的需求。 此程式代碼是提供給類別之較大程式碼範例的 IOrderedDictionary 一部分。
public ref class PeopleEnum : IDictionaryEnumerator
{
private:
// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position;
ArrayList^ _people;
public:
PeopleEnum(ArrayList^ list)
{
this->Reset();
_people = list;
}
virtual bool MoveNext()
{
position++;
return (position < _people->Count);
}
virtual void Reset()
{
position = -1;
}
virtual property Object^ Current
{
Object^ get()
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException^)
{
throw gcnew InvalidOperationException();
}
}
}
virtual property DictionaryEntry Entry
{
DictionaryEntry get()
{
return (DictionaryEntry)(Current);
}
}
virtual property Object^ Key
{
Object^ get()
{
try
{
return ((DictionaryEntry^)_people[position])->Key;
}
catch (IndexOutOfRangeException^)
{
throw gcnew InvalidOperationException();
}
}
}
virtual property Object^ Value
{
Object^ get()
{
try
{
return ((DictionaryEntry^)_people[position])->Value;
}
catch (IndexOutOfRangeException^)
{
throw gcnew InvalidOperationException();
}
}
}
};
public ref class People : IOrderedDictionary
{
private:
ArrayList^ _people;
public:
People(int numItems)
{
_people = gcnew ArrayList(numItems);
}
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;
}
virtual property Object^ default[Object^]
{
Object^ get(Object^ key)
{
return ((DictionaryEntry^)_people[IndexOfKey(key)])->Value;
}
void set(Object^ key, Object^ value)
{
_people[IndexOfKey(key)] = gcnew DictionaryEntry(key, value);
}
}
// IOrderedDictionary Members
virtual IDictionaryEnumerator^ GetEnumerator()
{
return gcnew PeopleEnum(_people);
}
virtual void Insert(int index, Object^ key, Object^ value)
{
if (IndexOfKey(key) != -1)
{
throw gcnew ArgumentException("An element with the same key already exists in the collection.");
}
_people->Insert(index, gcnew DictionaryEntry(key, value));
}
virtual void RemoveAt(int index)
{
_people->RemoveAt(index);
}
virtual property Object^ default[int]
{
Object^ get(int index)
{
return ((DictionaryEntry^)_people[index])->Value;
}
void set(int index, Object^ value)
{
Object^ key = ((DictionaryEntry^)_people[index])->Key;
_people[index] = gcnew DictionaryEntry(key, value);
}
}
// IDictionary Members
virtual void Add(Object^ key, Object^ value)
{
if (IndexOfKey(key) != -1)
{
throw gcnew ArgumentException("An element with the same key already exists in the collection.");
}
_people->Add(gcnew DictionaryEntry(key, value));
}
virtual void Clear()
{
_people->Clear();
}
virtual bool Contains(Object^ key)
{
if (IndexOfKey(key) == -1)
{
return false;
}
else
{
return true;
}
}
virtual property bool IsFixedSize
{
bool get()
{
return false;
}
}
virtual property bool IsReadOnly
{
bool get()
{
return false;
}
}
virtual property ICollection^ Keys
{
ICollection^ get()
{
ArrayList^ KeyCollection = gcnew ArrayList(_people->Count);
for (int i = 0; i < _people->Count; i++)
{
KeyCollection->Add( ((DictionaryEntry^)_people[i])->Key );
}
return KeyCollection;
}
}
virtual void Remove(Object^ key)
{
_people->RemoveAt(IndexOfKey(key));
}
virtual property ICollection^ Values
{
ICollection ^get()
{
ArrayList^ ValueCollection = gcnew ArrayList(_people->Count);
for (int i = 0; i < _people->Count; i++)
{
ValueCollection->Add( ((DictionaryEntry^)_people[i])->Value );
}
return ValueCollection;
}
}
// ICollection Members
virtual void CopyTo(Array^ array, int index)
{
_people->CopyTo(array, index);
}
virtual property int Count
{
int get()
{
return _people->Count;
}
}
virtual property bool IsSynchronized
{
bool get()
{
return _people->IsSynchronized;
}
}
virtual property Object^ SyncRoot
{
Object^ get()
{
return _people->SyncRoot;
}
}
// IEnumerable Members
virtual IEnumerator^ IfcGetEnumerator() = IEnumerable::GetEnumerator
{
return (IEnumerator^) gcnew PeopleEnum(_people);
}
};
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();
}
}
}
}
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
備註
C# 語言的 foreach
陳述式 (在 Visual Basic 中為 for each
) 會隱藏列舉值的複雜度。 因此,建議使用 foreach
,而不是直接操作列舉值。
列舉程式可以用來讀取集合中的資料,但是無法用來修改基礎集合。
一開始,列舉程式位在集合中的第一個項目之前。 Reset 也會將列舉值帶回至這個位置。 在這個位置, Current 屬性是未定義的。 因此,在讀取 MoveNext 的值之前,必須呼叫 Current 以將列舉值前移至集合的第一個項目。
Current 會傳回相同的物件直到呼叫 MoveNext 或 Reset。 MoveNext 會將 Current 設定為下一個項目。
如果 MoveNext 傳遞集合結尾,列舉值會放在集合的最後一個專案後面,並 MoveNext 傳 false
回 。 當列舉值位於這個位置時,後續呼叫 MoveNext 也會傳回 false
。 如果最後一 MoveNext 次呼叫傳 false
回 , Current 則為未定義。 若要再次將 Current 設定為集合的第一個元素,您可以在呼叫 Reset 之後,接著呼叫 MoveNext。
只要集合維持不變,列舉值就仍維持有效。 如果對集合進行變更,例如加入、修改或刪除項目,列舉程式會永久失效,且其行為未定義。
列舉程式沒有集合的獨佔存取權,因此,列舉集合內容本質上並不是安全的執行緒程序。 若要確保列舉期間的執行緒安全性,您可以在整個列舉期間鎖定集合。 若要讓多重執行緒能夠存取集合以便進行讀取和寫入,您必須實作自己的同步處理。