DictionaryEntry Struct
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Defines a dictionary key/value pair that can be set or retrieved.
public value class DictionaryEntry
public struct DictionaryEntry
[System.Serializable]
public struct DictionaryEntry
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct DictionaryEntry
type DictionaryEntry = struct
[<System.Serializable>]
type DictionaryEntry = struct
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type DictionaryEntry = struct
Public Structure DictionaryEntry
- Inheritance
- Attributes
The following example demonstrates the use of DictionaryEntry to iterate through a Hashtable object.
// A simple example for the DictionaryEntry structure.
using namespace System;
using namespace System::Collections;
public ref class Example
{
public:
static void Main()
{
// Create a new hash table.
//
Hashtable^ openWith = gcnew Hashtable();
// Add some elements to the hash table. 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");
// When you use foreach to enumerate hash table elements,
// the elements are retrieved as DictionaryEntry objects.
Console::WriteLine();
for each (DictionaryEntry de in openWith)
{
Console::WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
}
};
int main()
{
Example::Main();
}
/* This code example produces output similar to the following:
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
Key = dib, Value = paint.exe
Key = bmp, Value = paint.exe
*/
// A simple example for the DictionaryEntry structure.
using System;
using System.Collections;
class Example
{
public static void Main()
{
// Create a new hash table.
//
Hashtable openWith = new Hashtable();
// Add some elements to the hash table. 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");
// When you use foreach to enumerate hash table elements,
// the elements are retrieved as DictionaryEntry objects.
Console.WriteLine();
foreach (DictionaryEntry de in openWith)
{
Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
}
}
/* This code example produces output similar to the following:
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
Key = dib, Value = paint.exe
Key = bmp, Value = paint.exe
*/
'A simple example for the DictionaryEntry structure.
Imports System.Collections
Module Example
Sub Main()
' Create a new hash table.
'
Dim openWith As New Hashtable()
' Add some elements to the hash table. 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")
' When you use For Each to enumerate hash table elements,
' the elements are retrieved as DictionaryEntry objects.
Console.WriteLine()
For Each de As DictionaryEntry In openWith
Console.WriteLine("Key = {0}, Value = {1}", _
de.Key, de.Value)
Next de
End Sub
End Module
' This code example produces output similar to the following:
'
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe
'Key = dib, Value = paint.exe
'Key = bmp, Value = paint.exe
The IDictionaryEnumerator.Entry method returns an instance of this type.
Important
We don't recommend that you use the DictionaryEntry
structure for new development. Instead, we recommend that you use a generic KeyValuePair<TKey,TValue> structure along with the Dictionary<TKey,TValue> class. For more information, see Non-generic collections shouldn't be used on GitHub.
The C# foreach statement and the Visual Basic For Each statement require the type of each element in the collection. Since 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. Instead, the element type is DictionaryEntry. For example:
for each (DictionaryEntry de in openWith)
{
Console::WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
foreach (DictionaryEntry de in openWith)
{
Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
For Each de As DictionaryEntry In openWith
Console.WriteLine("Key = {0}, Value = {1}", _
de.Key, de.Value)
Next de
The foreach
statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.
Dictionary |
Initializes an instance of the DictionaryEntry type with the specified key and value. |
Deconstruct(Object, Object) |
Deconstructs the current DictionaryEntry. |
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: