IDictionaryとは? わかりやすく解説

IDictionary インターフェイス

キー/値ペアの非ジェネリック コレクション表します

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public Interface IDictionary
    Inherits ICollection, IEnumerable
[ComVisibleAttribute(true)] 
public interface IDictionary : ICollection, IEnumerable
[ComVisibleAttribute(true)] 
public interface class IDictionary : ICollection,
 IEnumerable
/** @attribute ComVisibleAttribute(true) */ 
public interface IDictionary extends ICollection, IEnumerable
ComVisibleAttribute(true) 
public interface IDictionary extends ICollection,
 IEnumerable
解説解説

IDictionary インターフェイスは、キー/値ペアの非ジェネリック コレクション対す基本インターフェイスです。このインターフェイスジェネリック バージョンについては、「System.Collections.Generic.IDictionary」を参照してください

各要素は、DictionaryEntry オブジェクト格納されているキー/値ペアです。

ペアが一意キー持っている必要がありますキーnull 参照 (Visual Basic では Nothing) にできるかどうかは、実装によって異なりますキーの値が null 参照 (Visual Basic では Nothing) になることを許容する場合は、キー一意にする必要はありません。IDictionary インターフェイス使用すると、保持されているキーと値を列挙できますが、特定の並べ替え順序指定できません。

IDictionary実装には、読み取り専用固定サイズ、および可変サイズ3 種類があります読み取り専用IDictionary オブジェクトは、変更できません。固定サイズIDictionary オブジェクトでは、要素追加または削除はできませんが、既存要素変更できます可変サイズIDictionary オブジェクトでは、要素追加削除、および変更できます

C# 言語foreach ステートメント (Visual Basic では for each) は、コレクション内の各要素の型を必要としますIDictionary オブジェクト各要素キー/値ペアであるため、要素の型は、キーの型や値の型にはなりません。その代わり要素の型は DictionaryEntryなります次に例を示します

foreach (DictionaryEntry de in myHashtable)
 {...}
For Each de As DictionaryEntry
 In myHashtable
  ...
Next myDE

foreach ステートメントは、列挙子のラッパーです。これは、コレクションからの読み取りだけを許可しコレクションへの書き込み防ぎます

実装時の注意 実装クラスは、キー比較する手段備えている必要があります

使用例使用例

IDictionary インターフェイス実装する単純なディクショナリ クラス定義する方法次のコード例示します

' This class implements a simple dictionary using an array of DictionaryEntry
 objects (key/value pairs).
Public Class SimpleDictionary
    Implements IDictionary

    ' The array of items
    Dim items() As DictionaryEntry
    Dim ItemsInUse As Integer
 = 0

    ' Construct the SimpleDictionary with the desired number of items.
    ' The number of items cannot change for the life time of this SimpleDictionary.
    Public Sub New(ByVal
 numItems As Integer)
        items = New DictionaryEntry(numItems - 1) {}
    End Sub

    ' IDictionary Members
    Public ReadOnly Property
 IsReadOnly() As Boolean Implements
 IDictionary.IsReadOnly
        Get
            Return False
        End Get
    End Property

    Public Function Contains(ByVal
 key As Object) As Boolean
 Implements IDictionary.Contains
        Dim index As Integer
        Return TryGetIndexOfKey(key, index)
    End Function

    Public ReadOnly Property
 IsFixedSize() As Boolean Implements
 IDictionary.IsFixedSize
        Get
            Return False
        End Get
    End Property

    Public Sub Remove(ByVal
 key As Object) Implements
 IDictionary.Remove
        If key = Nothing Then
            Throw New ArgumentNullException("key")
        End If
        ' Try to find the key in the DictionaryEntry array
        Dim index As Integer
        If TryGetIndexOfKey(key, index) Then

            ' If the key is found, slide all the items up.
            Array.Copy(items, index + 1, items, index, (ItemsInUse - index) - 1)
            ItemsInUse = ItemsInUse - 1
        Else

            ' If the key is not in the dictionary, just return. 
        End If
    End Sub

    Public Sub Clear() Implements
 IDictionary.Clear
        ItemsInUse = 0
    End Sub

    Public Sub Add(ByVal
 key As Object, ByVal value
 As Object) Implements IDictionary.Add

        ' Add the new key/value pair even if this key already exists
 in the dictionary.
        If ItemsInUse = items.Length Then
            Throw New InvalidOperationException("The
 dictionary cannot hold any more items.")
        End If
        items(ItemsInUse) = New DictionaryEntry(key, value)
        ItemsInUse = ItemsInUse + 1
    End Sub

    Public ReadOnly Property
 Keys() As ICollection Implements IDictionary.Keys
        Get

            ' Return an array where each item is a key.
            Dim keyArray() As Object
 = New Object(ItemsInUse - 1) {}
            Dim n As Integer
            For n = 0 To ItemsInUse - 1
                keyArray(n) = items(n).Key
            Next n

            Return keyArray
        End Get
    End Property

    Public ReadOnly Property
 Values() As ICollection Implements IDictionary.Values
        Get
            ' Return an array where each item is a value.
            Dim valueArray() As Object
 = New Object(ItemsInUse - 1) {}
            Dim n As Integer
            For n = 0 To ItemsInUse - 1
                valueArray(n) = items(n).Value
            Next n

            Return valueArray
        End Get
    End Property

    Public Property Item(ByVal
 key As Object) As Object
 Implements IDictionary.Item
        Get

            ' If this key is in the dictionary, return its value.
            Dim index As Integer
            If TryGetIndexOfKey(key, index) Then

                ' The key was found return its value.
                Return items(index).Value
            Else

                ' The key was not found return null.
                Return Nothing
            End If
        End Get

        Set(ByVal value As
 Object)
            ' If this key is in the dictionary, change its value. 
            Dim index As Integer
            If TryGetIndexOfKey(key, index) Then

                ' The key was found change its value.
                items(index).Value = value
            Else

                ' This key is not in the dictionary add this key/value
 pair.
                Add(key, value)
            End If
        End Set
    End Property

    Private Function TryGetIndexOfKey(ByVal
 key As Object, ByRef index
 As Integer) As Boolean
        For index = 0 To ItemsInUse - 1
            ' If the key is found, return true (the index is also returned).
            If items(index).Key.Equals(key) Then
                Return True
            End If
        Next index

        ' Key not found, return false (index should be ignored by the
 caller).
        Return False
    End Function
// This class implements a simple dictionary using an array of DictionaryEntry
 objects (key/value pairs).
public class SimpleDictionary : IDictionary
{
    // The array of items
    private DictionaryEntry[] items;
    private Int32 ItemsInUse = 0;

    // Construct the SimpleDictionary with the desired number of items.
    // The number of items cannot change for the life time of this SimpleDictionary.
    public SimpleDictionary(Int32 numItems)
    {
        items = new DictionaryEntry[numItems];
    }


    #region IDictionary Members
    public bool IsReadOnly { get
 { return false; } }
    public bool Contains(object key)
    {
       Int32 index;
       return TryGetIndexOfKey(key, out index);
    }
    public bool IsFixedSize { get
 { return false; } }
    public void Remove(object key)
    {
        if (key == null) throw new
 ArgumentNullException("key");
        // Try to find the key in the DictionaryEntry array
        Int32 index;
        if (TryGetIndexOfKey(key, out index))
        {
            // If the key is found, slide all the items up.
            Array.Copy(items, index + 1, items, index, ItemsInUse - index - 1);
            ItemsInUse--;
        } 
        else
        {
            // If the key is not in the dictionary, just return. 
        }
    }
    public void Clear() { ItemsInUse = 0; }
    public void Add(object key, object value)
 
    {
        // Add the new key/value pair even if this key already exists
 in the dictionary.
        if (ItemsInUse == items.Length)
            throw new InvalidOperationException("The dictionary
 cannot hold any more items.");
        items[ItemsInUse++] = new DictionaryEntry(key, value);
    }
    public ICollection Keys
    {
        get
        {
            // Return an array where each item is a key.
            Object[] keys = new Object[ItemsInUse];
            for (Int32 n = 0; n < ItemsInUse; n++)
                keys[n] = items[n].Key;
            return keys;
        }
    }
    public ICollection Values
    {
        get
        {
            // Return an array where each item is a value.
            Object[] values = new Object[ItemsInUse];
            for (Int32 n = 0; n < ItemsInUse; n++)
                values[n] = items[n].Value;
            return values;
        }
    }
    public object this[object key]
    {
        get
        {   
            // If this key is in the dictionary, return its value.
            Int32 index;
            if (TryGetIndexOfKey(key, out index))
            {
                // The key was found; return its value.
                return items[index].Value;
            } 
            else
            {
                // The key was not found; return null.
                return null;
            }
        }

        set
        {
            // If this key is in the dictionary, change its value. 
            Int32 index;
            if (TryGetIndexOfKey(key, out index))
            {
                // The key was found; change its value.
                items[index].Value = value;
            } 
            else
            {
                // This key is not in the dictionary; add this key/value
 pair.
                Add(key, value);
            }
        }
    }
    private Boolean TryGetIndexOfKey(Object key, out Int32 index)
    {
        for (index = 0; index < ItemsInUse; index++)
        {
            // If the key is found, return true (the index is also returned).
            if (items[index].Key.Equals(key)) return
 true;
        }
      
        // Key not found, return false (index should be ignored by the
 caller).
        return false;
    }
    private class SimpleDictionaryEnumerator
 : IDictionaryEnumerator
    {
        // A copy of the SimpleDictionary object's key/value pairs.
        DictionaryEntry[] items;
        Int32 index = -1;

        public SimpleDictionaryEnumerator(SimpleDictionary sd)
        {
            // Make a copy of the dictionary entries currently in the
 SimpleDictionary object.
            items = new DictionaryEntry[sd.Count];
            Array.Copy(sd.items, 0, items, 0, sd.Count);
        }

        // Return the current item.
        public Object Current { get { ValidateIndex();
 return items[index]; } }

        // Return the current dictionary entry.
        public DictionaryEntry Entry
        {
            get { return (DictionaryEntry)
 Current; }
        }

        // Return the key of the current item.
        public Object Key { get { ValidateIndex();
  return items[index].Key; } }

        // Return the value of the current item.
        public Object Value { get { ValidateIndex();
  return items[index].Value; } }

        // Advance to the next item.
        public Boolean MoveNext()
        {
            if (index < items.Length - 1) { index++; return
 true; }
            return false;
        }

        // Validate the enumeration index and throw an exception if
 the index is out of range.
        private void ValidateIndex()
        {
            if (index < 0 || index >= items.Length)
            throw new InvalidOperationException("Enumerator
 is before or after the collection.");
        }

        // Reset the index to restart the enumeration.
        public void Reset()
        {
            index = -1;
        }
    }
    public IDictionaryEnumerator GetEnumerator()
    {
        // Construct and return an enumerator.
        return new SimpleDictionaryEnumerator(this);
    }
    #endregion

    #region ICollection Members
    public bool IsSynchronized { get
 { return false; } }
    public object SyncRoot { get { throw new
 NotImplementedException(); } }
    public int Count { get
 { return ItemsInUse; } }
    public void CopyTo(Array array, int
 index) { throw new NotImplementedException(); }
    #endregion

    #region IEnumerable Members
    IEnumerator IEnumerable.GetEnumerator() 
    {
        // Construct and return an enumerator.
        return ((IDictionary)this).GetEnumerator();
    }
    #endregion
}
// This class implements a simple dictionary using an array of
// DictionaryEntry objects (key/value pairs).
public ref class SimpleDictionary : public
 IDictionary
{
    // The array of items
private:
    array<DictionaryEntry^>^ items;
private:
    int itemsInUse;

    // Construct the SimpleDictionary with the desired number of
    // items. The number of items cannot change for the life time of
    // this SimpleDictionary.
public:
    SimpleDictionary(int size)
    {
        items = gcnew array<DictionaryEntry^>(size);
    }

    #pragma region IDictionary Members
public:
    property virtual bool IsReadOnly
    {
        bool get()
        {
            return false;
        }
    }
public:
    virtual bool Contains(Object^ key)
    {
        int index;
        return TryGetIndexOfKey(key, &index);
    }
public:
    virtual property bool IsFixedSize
    {
        bool get()
        {
            return false;
        }
    }
public:
    virtual void Remove(Object^ key)
    {
        if (key == nullptr)
        {
            throw gcnew ArgumentNullException("key");
        }
        // Try to find the key in the DictionaryEntry array
        int index;
        if (TryGetIndexOfKey(key, &index))
        {
            // If the key is found, slide all the items down.
            Array::Copy(items, index + 1, items, index, itemsInUse -
                index - 1);
            itemsInUse--;
        }
        else
        {
            // If the key is not in the dictionary, just return.
            return;
        }
    }
public:
    virtual void Clear()
    {
        itemsInUse = 0;
    }
public:
    virtual void Add(Object^ key, Object^ value)
    {
        // Add the new key/value pair even if this key already exists
        // in the dictionary.
        if (itemsInUse == items->Length)
        {
            throw gcnew InvalidOperationException
                ("The dictionary cannot hold any more items.");
        }
        items[itemsInUse++] = gcnew DictionaryEntry(key, value);
    }
public:
    virtual property ICollection^ Keys
    {
        ICollection^ get()
        {
            // Return an array where each item is a key.
            array<Object^>^ keys = gcnew array<Object^>(itemsInUse);
            for (int i = 0; i < itemsInUse;
 i++)
            {
                keys[i] = items[i]->Key;
            }
            return keys;
        }
    }
public:
    virtual property ICollection^ Values
    {
        ICollection^ get()
        {
            // Return an array where each item is a value.
            array<Object^>^ values = gcnew array<Object^>(itemsInUse);
            for (int i = 0; i < itemsInUse;
 i++)
            {
                values[i] = items[i]->Value;
            }
            return values;
        }
    }
public:
    virtual property Object^ default[Object^]
    {
        Object^ get(Object^ key)
        {
            // If this key is in the dictionary, return its value.
            int index;
            if (TryGetIndexOfKey(key, &index))
            {
                // The key was found; return its value.
                return items[index]->Value;
            }
            else
            {
                // The key was not found; return null.
                return nullptr;
            }
        }

        void set(Object^ key, Object^ value)
        {
            // If this key is in the dictionary, change its value.
            int index;
            if (TryGetIndexOfKey(key, &index))
            {
                // The key was found; change its value.
                items[index]->Value = value;
            }
            else
            {
                // This key is not in the dictionary; add this
                // key/value pair.
                Add(key, value);
            }
        }
    }
private:
    bool TryGetIndexOfKey(Object^ key, int*
 index)
    {
        for (*index = 0; *index < itemsInUse; *index++)
        {
            // If the key is found, return true (the index is also
            // returned).
            if (items[*index]->Key->Equals(key))
            {
                return true;
            }
        }

        // Key not found, return false (index should be ignored by
        // the caller).
        return false;
    }
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

IDictionary ジェネリック インターフェイス

メモ : このインターフェイスは、.NET Framework version 2.0新しく追加されたものです。

キーと値のペアジェネリック コレクション表します

名前空間: System.Collections.Generic
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Interface IDictionary(Of
 TKey, TValue)
    Inherits ICollection(Of KeyValuePair(Of
 TKey, TValue)), IEnumerable(Of KeyValuePair(Of
 TKey, TValue)), _
    IEnumerable
Dim instance As IDictionary(Of
 TKey, TValue)
public interface IDictionary<TKey,TValue> : ICollection<KeyValuePair<TKey,TValue>>,
 IEnumerable<KeyValuePair<TKey,TValue>>, 
    IEnumerable
generic<typename TKey, typename TValue>
public interface class IDictionary : ICollection<KeyValuePair<TKey,
 TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, 
    IEnumerable
J# では、ジェネリックな型およびメソッド使用できますが、新規に宣言することはできません。
JScript では、ジェネリックな型およびメソッド使用できません。

型パラメータ

TKey

ディクショナリ内のキーの型。

TValue

ディクショナリ内の値の型。

解説解説

IDictionary インターフェイスは、キーと値のペアジェネリック コレクション対す基本インターフェイスです。

各要素は、KeyValuePair オブジェクト格納されているキーと値のペアです。

ペアが一意キー持っている必要がありますkeynull 参照 (Visual Basic では Nothing) にできるかどうかは、実装によって異なります。値は null 参照 (Visual Basic では Nothing) にすることができ、一意である必要はありません。IDictionary インターフェイス使用すると、保持されているキーと値を列挙できますが、特定の並べ替え順序指定できません。

C# 言語foreach ステートメント (Visual Basic の場合For EachC++ の場合 for each) は、コレクション内の各要素の型を必要としますIDictionary各要素キーと値のペアであるため、要素の型は、キーの型や値の型にはなりません。その代わり要素の型は KeyValuePairなります次に例を示します

foreach (KeyValuePair<int, string>
 kvp in myDictionary) {...}
for each (KeyValuePair<int, String^>
 kvp in myDictionary) {...}
For Each kvp As KeyValuePair(Of
 Integer, String) In myDictionary
    ...
Next kvp

foreach ステートメントは、列挙子のラッパーです。これは、コレクションからの読み取りだけを許可しコレクションへの書き込み防ぎます

実装時の注意 実装クラスは、キー比較する手段備えている必要があります

使用例使用例

整数キー使用して文字列の空の Dictionary作成しIDictionary インターフェイスを介してアクセスするコード例次に示します

このコード例では、Add メソッド使用していくつかの要素追加します。この例では、重複するキー追加しようとすると、Add メソッドが ArgumentException をスローすることを示します

この例では、Item プロパティ (C# ではインデクサ) を使用して値を取得し要求されキー存在しないときに KeyNotFoundException がスローされる例を示し、またキー関連付けられた値を置き換えることができること示します

この例では、プログラムがディクショナリにないキー値を頻繁に試行する必要がある場合に、より効率的に値を取得する方法として TryGetValue メソッド使用する方法、および ContainsKey メソッド使用してAdd メソッド呼び出す前にキー存在するかどうかテストする方法示します

最後に、この例では、ディクショナリのキーと値を列挙する方法、および Values プロパティ使用して値のみを列挙する方法示します

Imports System
Imports System.Collections.Generic

Public Class Example
    
    Public Shared Sub Main()
 

        ' Create a new dictionary of strings, with string keys, 
        ' and access it through the IDictionary generic interface.
        Dim openWith As IDictionary(Of
 String, String) = _
            New Dictionary(Of String,
 String)
        
        ' Add some elements to the dictionary. There are no 
        ' duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe")
        openWith.Add("bmp", "paint.exe")
        openWith.Add("dib", "paint.exe")
        openWith.Add("rtf", "wordpad.exe")
        
        ' The Add method throws an exception if the new key is 
        ' already in the dictionary.
        Try
            openWith.Add("txt", "winword.exe")
        Catch 
            Console.WriteLine("An element with Key = ""txt""
 already exists.")
        End Try

        ' The Item property is the default property, so you 
        ' can omit its name when accessing elements. 
        Console.WriteLine("For key = ""rtf"",
 value = {0}.", _
            openWith("rtf"))
        
        ' The default Item property can be used to change the value
        ' associated with a key.
        openWith("rtf") = "winword.exe"
        Console.WriteLine("For key = ""rtf"",
 value = {0}.", _
            openWith("rtf"))
        
        ' If a key does not exist, setting the default item property
        ' for that key adds a new key/value pair.
        openWith("doc") = "winword.exe"

        ' The default Item property throws an exception if the requested
        ' key is not in the dictionary.
        Try
            Console.WriteLine("For key = ""tif"",
 value = {0}.", _
                openWith("tif"))
        Catch 
            Console.WriteLine("Key = ""tif""
 is not found.")
        End Try

        ' When a program often has to try keys that turn out not to
        ' be in the dictionary, TryGetValue can be a more efficient
 
        ' way to retrieve values.
        Dim value As String
 = ""
        If openWith.TryGetValue("tif",
 value) Then
            Console.WriteLine("For key = ""tif"",
 value = {0}.", value)
        Else
            Console.WriteLine("Key = ""tif""
 is not found.")
        End If

        ' ContainsKey can be used to test keys before inserting 
        ' them.
        If Not openWith.ContainsKey("ht")
 Then
            openWith.Add("ht", "hypertrm.exe")
            Console.WriteLine("Value added for key = ""ht"":
 {0}", _
                openWith("ht"))
        End If

        ' When you use foreach to enumerate dictionary elements,
        ' the elements are retrieved as KeyValuePair objects.
        Console.WriteLine()
        For Each kvp As
 KeyValuePair(Of String, String)
 In openWith
            Console.WriteLine("Key = {0}, Value = {1}",
 _
                kvp.Key, kvp.Value)
        Next kvp

        ' To get the values alone, use the Values property.
        Dim icoll As ICollection(Of
 String) = openWith.Values
        
        ' The elements of the ValueCollection are strongly typed
        ' with the type that was specified for dictionary values.
        Console.WriteLine()
        For Each s As String
 In  icoll
            Console.WriteLine("Value = {0}", s)
        Next s

        ' To get the keys alone, use the Keys property.
        icoll = openWith.Keys
        
        ' The elements of the ValueCollection are strongly typed
        ' with the type that was specified for dictionary values.
        Console.WriteLine()
        For Each s As String
 In  icoll
            Console.WriteLine("Key = {0}", s)
        Next s

        ' Use the Remove method to remove a key/value pair.
        Console.WriteLine(vbLf + "Remove(""doc"")")
        openWith.Remove("doc")
        
        If Not openWith.ContainsKey("doc")
 Then
            Console.WriteLine("Key ""doc""
 is not found.")
        End If

    End Sub

End Class

' This code example produces the following output:
'
'An element with Key = "txt" already exists.
'For key = "rtf", value = wordpad.exe.
'For key = "rtf", value = winword.exe.
'Key = "tif" is not found.
'Key = "tif" is not found.
'Value added for key = "ht": hypertrm.exe
'
'Key = txt, Value = notepad.exe
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = winword.exe
'Key = doc, Value = winword.exe
'Key = ht, Value = hypertrm.exe
'
'Value = notepad.exe
'Value = paint.exe
'Value = paint.exe
'Value = winword.exe
'Value = winword.exe
'Value = hypertrm.exe
'
'Key = txt
'Key = bmp
'Key = dib
'Key = rtf
'Key = doc
'Key = ht
'
'Remove("doc")
'Key "doc" is not found.
' 
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create a new dictionary of strings, with string keys, 
        // and access it through the IDictionary generic interface.
        IDictionary<string, string> openWith
 = 
            new Dictionary<string, string>();

        // Add some elements to the dictionary. There are no 
        // duplicate keys, but some of the values are duplicates.
        openWith.Add("txt", "notepad.exe");
        openWith.Add("bmp", "paint.exe");
        openWith.Add("dib", "paint.exe");
        openWith.Add("rtf", "wordpad.exe");

        // The Add method throws an exception if the new key is 
        // already in the dictionary.
        try
        {
            openWith.Add("txt", "winword.exe");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("An element with Key = \"txt\" already
 exists.");
        }

        // The Item property is another name for the indexer, so you
 
        // can omit its name when accessing elements. 
        Console.WriteLine("For key = \"rtf\", value = {0}.",
 
            openWith["rtf"]);

        // The indexer can be used to change the value associated
        // with a key.
        openWith["rtf"] = "winword.exe";
        Console.WriteLine("For key = \"rtf\", value = {0}.",
 
            openWith["rtf"]);

        // If a key does not exist, setting the indexer for that key
        // adds a new key/value pair.
        openWith["doc"] = "winword.exe";

        // The indexer throws an exception if the requested key is
        // not in the dictionary.
        try
        {
            Console.WriteLine("For key = \"tif\", value = {0}.",
 
                openWith["tif"]);
        }
        catch (KeyNotFoundException)
        {
            Console.WriteLine("Key = \"tif\" is not found.");
        }

        // When a program often has to try keys that turn out not to
        // be in the dictionary, TryGetValue can be a more efficient
 
        // way to retrieve values.
        string value = "";
        if (openWith.TryGetValue("tif", out value))
        {
            Console.WriteLine("For key = \"tif\", value = {0}.",
 value);
        }
        else
        {
            Console.WriteLine("Key = \"tif\" is not found.");
        }

        // ContainsKey can be used to test keys before inserting 
        // them.
        if (!openWith.ContainsKey("ht"))
        {
            openWith.Add("ht", "hypertrm.exe");
            Console.WriteLine("Value added for key = \"ht\":
 {0}", 
                openWith["ht"]);
        }

        // When you use foreach to enumerate dictionary elements,
        // the elements are retrieved as KeyValuePair objects.
        Console.WriteLine();
        foreach( KeyValuePair<string, string>
 kvp in openWith )
        {
            Console.WriteLine("Key = {0}, Value = {1}", 
                kvp.Key, kvp.Value);
        }

        // To get the values alone, use the Values property.
        ICollection<string> icoll = openWith.Values;

        // The elements of the ValueCollection are strongly typed
        // with the type that was specified for dictionary values.
        Console.WriteLine();
        foreach( string s in
 icoll )
        {
            Console.WriteLine("Value = {0}", s);
        }

        // To get the keys alone, use the Keys property.
        icoll = openWith.Keys;

        // The elements of the ValueCollection are strongly typed
        // with the type that was specified for dictionary values.
        Console.WriteLine();
        foreach( string s in
 icoll )
        {
            Console.WriteLine("Key = {0}", s);
        }

        // Use the Remove method to remove a key/value pair.
        Console.WriteLine("\nRemove(\"doc\")");
        openWith.Remove("doc");

        if (!openWith.ContainsKey("doc"))
        {
            Console.WriteLine("Key \"doc\" is not found.");
        }
    }
}

/* This code example produces the following output:

An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Key = "tif" is not found.
Key = "tif" is not found.
Value added for key = "ht": hypertrm.exe

Key = txt, Value = notepad.exe
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = winword.exe
Key = doc, Value = winword.exe
Key = ht, Value = hypertrm.exe

Value = notepad.exe
Value = paint.exe
Value = paint.exe
Value = winword.exe
Value = winword.exe
Value = hypertrm.exe

Key = txt
Key = bmp
Key = dib
Key = rtf
Key = doc
Key = ht

Remove("doc")
Key "doc" is not found.
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

IDictionary プロパティ


IDictionary プロパティ


IDictionary メソッド


IDictionary メソッド


IDictionary メンバ


IDictionary メンバ




英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「IDictionary」の関連用語

IDictionaryのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



IDictionaryのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS