IOrderedDictionary 인터페이스
정의
키/값 쌍의 인덱싱된 컬렉션을 나타냅니다.Represents an indexed collection of key/value pairs.
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
- 파생
- 구현
예제
다음 코드 예제에서는 클래스를 기반으로 하는 간단한의 구현을 보여 줍니다 IOrderedDictionary ArrayList .The following code example demonstrates the implementation of a simple IOrderedDictionary based on the ArrayList class. 구현 된는 첫 번째 이름을 IOrderedDictionary 키와 성으로 값으로 저장 하 고, 각 이름에 고유한 요구 사항이 추가 됩니다.The implemented IOrderedDictionary stores first names as the keys and last names as the values, with the added requirement that each first name is unique.
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
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);
}
};
class App
{
public:
static void Main()
{
People^ peopleCollection = gcnew People(3);
peopleCollection->Add("John", "Smith");
peopleCollection->Add("Jim", "Johnson");
peopleCollection->Add("Sue", "Rabon");
Console::WriteLine("Displaying the entries in peopleCollection:");
for each (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");
for each (DictionaryEntry^ de in peopleCollection)
{
Console::WriteLine("{0} {1}", de->Key, de->Value);
}
}
};
int main()
{
App::Main();
}
/* 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
*/
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
설명
IOrderedDictionary 요소는 키 또는 인덱스를 사용 하 여 액세스할 수 있습니다.IOrderedDictionary elements can be accessed either with the key or with the index.
각 요소는 구조체에 저장 된 키/값 쌍입니다 DictionaryEntry .Each element is a key/value pair stored in a DictionaryEntry structure.
각 쌍에는 아닌 고유 키가 있어야 null
하지만 값은 일 수 있으며 null
고유할 필요가 없습니다.Each pair must have a unique key that is not null
, but the value can be null
and does not have to be unique. IOrderedDictionary인터페이스를 사용 하면 포함 된 키와 값을 열거할 수 있지만 특정 정렬 순서를 의미 하지는 않습니다.The IOrderedDictionary interface allows the contained keys and values to be enumerated, but it does not imply any particular sort order.
foreach
C # 언어 ( For Each
Visual Basic)의 문은 컬렉션의 요소 형식에 대 한 개체를 반환 합니다.The foreach
statement of the C# language (For Each
in Visual Basic) returns an object of the type of the elements in the collection. 의 각 요소는 IDictionary 키/값 쌍 이므로 요소 형식은 키의 형식이 나 값의 형식이 아닙니다.Because each element of the IDictionary is a key/value pair, the element type is not the type of the key or the type of the value. 대신, DictionaryEntry 다음 예제와 같이 요소 형식은입니다.Instead, the element type is DictionaryEntry, as the following example shows.
for each (DictionaryEntry de in myOrderedDictionary)
{
//...
}
foreach (DictionaryEntry de in myOrderedDictionary)
{
//...
}
For Each de In myOrderedDictionary
'...
Next
foreach
문은 컬렉션에 쓰지 않고 읽을 수 있는 열거자에 대 한 래퍼입니다.The foreach
statement is a wrapper around the enumerator, which allows only reading from, not writing to, the collection.
구현자 참고
구현 하는 클래스에는 키를 비교 하는 방법이 있어야 합니다.The implementing class must have a means to compare keys.
속성
Count |
ICollection에 포함된 요소 수를 가져옵니다.Gets the number of elements contained in the ICollection. (다음에서 상속됨 ICollection) |
IsFixedSize |
IDictionary 개체의 크기가 고정되어 있는지 여부를 나타내는 값을 가져옵니다.Gets a value indicating whether the IDictionary object has a fixed size. (다음에서 상속됨 IDictionary) |
IsReadOnly |
IDictionary 개체가 읽기 전용인지 여부를 나타내는 값을 가져옵니다.Gets a value indicating whether the IDictionary object is read-only. (다음에서 상속됨 IDictionary) |
IsSynchronized |
ICollection에 대한 액세스가 동기화되어 스레드로부터 안전하게 보호되는지를 나타내는 값을 가져옵니다.Gets a value indicating whether access to the ICollection is synchronized (thread safe). (다음에서 상속됨 ICollection) |
Item[Int32] |
지정한 인덱스에 있는 요소를 가져오거나 설정합니다.Gets or sets the element at the specified index. |
Item[Object] |
지정한 키를 가진 요소를 가져오거나 설정합니다.Gets or sets the element with the specified key. (다음에서 상속됨 IDictionary) |
Keys |
ICollection 개체의 키를 포함하는 IDictionary 개체를 가져옵니다.Gets an ICollection object containing the keys of the IDictionary object. (다음에서 상속됨 IDictionary) |
SyncRoot |
ICollection에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다.Gets an object that can be used to synchronize access to the ICollection. (다음에서 상속됨 ICollection) |
Values |
ICollection 개체의 값이 포함된 IDictionary 개체를 가져옵니다.Gets an ICollection object containing the values in the IDictionary object. (다음에서 상속됨 IDictionary) |
메서드
Add(Object, Object) |
제공된 키와 값을 가진 요소를 IDictionary 개체에 추가합니다.Adds an element with the provided key and value to the IDictionary object. (다음에서 상속됨 IDictionary) |
Clear() |
IDictionary 개체에서 요소를 모두 제거합니다.Removes all elements from the IDictionary object. (다음에서 상속됨 IDictionary) |
Contains(Object) |
IDictionary 개체에 지정한 키를 가진 요소가 포함되어 있는지 여부를 결정합니다.Determines whether the IDictionary object contains an element with the specified key. (다음에서 상속됨 IDictionary) |
CopyTo(Array, Int32) |
특정 ICollection 인덱스부터 시작하여 Array의 요소를 Array에 복사합니다.Copies the elements of the ICollection to an Array, starting at a particular Array index. (다음에서 상속됨 ICollection) |
GetEnumerator() |
IOrderedDictionary 컬렉션에서 반복하는 열거자를 반환합니다.Returns an enumerator that iterates through the IOrderedDictionary collection. |
Insert(Int32, Object, Object) |
컬렉션의 지정된 인덱스에 키/값 쌍을 삽입합니다.Inserts a key/value pair into the collection at the specified index. |
Remove(Object) |
IDictionary 개체에서 지정한 키를 가지는 요소를 제거합니다.Removes the element with the specified key from the IDictionary object. (다음에서 상속됨 IDictionary) |
RemoveAt(Int32) |
지정된 인덱스에 있는 요소를 제거합니다.Removes the element at the specified index. |
확장 메서드
Cast<TResult>(IEnumerable) |
IEnumerable의 요소를 지정된 형식으로 캐스팅합니다.Casts the elements of an IEnumerable to the specified type. |
OfType<TResult>(IEnumerable) |
지정된 형식에 따라 IEnumerable의 요소를 필터링합니다.Filters the elements of an IEnumerable based on a specified type. |
AsParallel(IEnumerable) |
쿼리를 병렬화할 수 있도록 합니다.Enables parallelization of a query. |
AsQueryable(IEnumerable) |
IEnumerable을 IQueryable로 변환합니다.Converts an IEnumerable to an IQueryable. |