Class MapField<TKey, TValue>
Representation of a map field in a Protocol Buffer message.
Inheritance
Implements
Inherited Members
Namespace: Google.Protobuf.Collections
Assembly: Google.Protobuf.dll
Syntax
public sealed class MapField<TKey, TValue> : IDeepCloneable<MapField<TKey, TValue>>, IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEquatable<MapField<TKey, TValue>>, IDictionary, ICollection, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
Type Parameters
Name | Description |
---|---|
TKey | Key type in the map. Must be a type supported by Protocol Buffer map keys. |
TValue | Value type in the map. Must be a type supported by Protocol Buffers. |
Remarks
For string keys, the equality comparison is provided by System.StringComparer.Ordinal.
Null values are not permitted in the map, either for wrapper types or regular messages. If a map is deserialized from a data stream and the value is missing from an entry, a default value is created instead. For primitive types, that is the regular default value (0, the empty string and so on); for message types, an empty instance of the message is created, as if the map entry contained a 0-length encoded value for the field.
This implementation does not generally prohibit the use of key/value types which are not supported by Protocol Buffers (e.g. using a key type of
byte
) but nor does it guarantee
that all operations will work in such cases.
The order in which entries are returned when iterating over this object is undefined, and may change in future versions.
Properties
Count
Gets the number of elements contained in the map.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Implements
IsReadOnly
Gets a value indicating whether the map is read-only.
Declaration
public bool IsReadOnly { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Implements
Item[TKey]
Gets or sets the value associated with the specified key.
Declaration
public TValue this[TKey key] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key of the value to get or set. |
Property Value
Type | Description |
---|---|
TValue | The value associated with the specified key. If the specified key is not found, a get operation throws a System.Collections.Generic.KeyNotFoundException, and a set operation creates a new element with the specified key. |
Implements
Exceptions
Type | Condition |
---|---|
System.Collections.Generic.KeyNotFoundException | The property is retrieved and key does not exist in the collection. |
Keys
Gets a collection containing the keys in the map.
Declaration
public ICollection<TKey> Keys { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.ICollection<TKey> |
Implements
Values
Gets a collection containing the values in the map.
Declaration
public ICollection<TValue> Values { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.ICollection<TValue> |
Implements
Methods
Add(TKey, TValue)
Adds the specified key/value pair to the map.
Declaration
public void Add(TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to add |
TValue | value | The value to add. |
Implements
Remarks
This operation fails if the key already exists in the map. To replace an existing entry, use the indexer.
Exceptions
Type | Condition |
---|---|
System.ArgumentException | The given key already exists in map. |
Add(IDictionary<TKey, TValue>)
Adds the specified entries to the map. The keys and values are not automatically cloned.
Declaration
public void Add(IDictionary<TKey, TValue> entries)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<TKey, TValue> | entries | The entries to add to the map. |
AddEntriesFrom(CodedInputStream, MapField<TKey, TValue>.Codec)
Adds entries to the map from the given stream.
Declaration
public void AddEntriesFrom(CodedInputStream input, MapField<TKey, TValue>.Codec codec)
Parameters
Type | Name | Description |
---|---|---|
CodedInputStream | input | Stream to read from |
MapField.Codec<> | codec | Codec describing how the key/value pairs are encoded |
Remarks
It is assumed that the stream is initially positioned after the tag specified by the codec. This method will continue reading entries from the stream until the end is reached, or a different tag is encountered.
CalculateSize(MapField<TKey, TValue>.Codec)
Calculates the size of this map based on the given entry codec.
Declaration
public int CalculateSize(MapField<TKey, TValue>.Codec codec)
Parameters
Type | Name | Description |
---|---|---|
MapField.Codec<> | codec | The codec to use to encode each entry. |
Returns
Type | Description |
---|---|
System.Int32 |
Clear()
Removes all items from the map.
Declaration
public void Clear()
Implements
Clone()
Creates a deep clone of this object.
Declaration
public MapField<TKey, TValue> Clone()
Returns
Type | Description |
---|---|
MapField<TKey, TValue> | A deep clone of this object. |
Implements
ContainsKey(TKey)
Determines whether the specified key is present in the map.
Declaration
public bool ContainsKey(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to check. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Implements
Equals(MapField<TKey, TValue>)
Compares this map with another for equality.
Declaration
public bool Equals(MapField<TKey, TValue> other)
Parameters
Type | Name | Description |
---|---|---|
MapField<TKey, TValue> | other | The map to compare this with. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Implements
Remarks
The order of the key/value pairs in the maps is not deemed significant in this comparison.
Equals(Object)
Determines whether the specified System.Object, is equal to this instance.
Declaration
public override bool Equals(object other)
Parameters
Type | Name | Description |
---|---|---|
System.Object | other | The System.Object to compare with this instance. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Overrides
GetEnumerator()
Returns an enumerator that iterates through the collection.
Declaration
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<TKey, TValue>> | An enumerator that can be used to iterate through the collection. |
Implements
GetHashCode()
Returns a hash code for this instance.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
System.Int32 | A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. |
Overrides
Remove(TKey)
Removes the entry identified by the given key from the map.
Declaration
public bool Remove(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key indicating the entry to remove from the map. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Implements
ToString()
Returns a string representation of this repeated field, in the same way as it would be represented by the default JSON formatter.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
System.String |
Overrides
TryGetValue(TKey, out TValue)
Gets the value associated with the specified key.
Declaration
public bool TryGetValue(TKey key, out TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key whose value to get. |
TValue | value | When this method returns, the value associated with the specified key, if the key is found;
otherwise, the default value for the type of the |
Returns
Type | Description |
---|---|
System.Boolean |
|
Implements
WriteTo(CodedOutputStream, MapField<TKey, TValue>.Codec)
Writes the contents of this map to the given coded output stream, using the specified codec to encode each entry.
Declaration
public void WriteTo(CodedOutputStream output, MapField<TKey, TValue>.Codec codec)
Parameters
Type | Name | Description |
---|---|---|
CodedOutputStream | output | The output stream to write to. |
MapField.Codec<> | codec | The codec to use for each entry. |
Explicit Interface Implementations
ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue>)
Adds the specified item to the map.
Declaration
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.KeyValuePair<TKey, TValue> | item | The item to add to the map. |
Implements
ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue>)
Determines whether map contains an entry equivalent to the given key/value pair.
Declaration
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.KeyValuePair<TKey, TValue> | item | The key/value pair to find. |
Returns
Type | Description |
---|---|
System.Boolean |
Implements
ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[], Int32)
Copies the key/value pairs in this map to an array.
Declaration
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.KeyValuePair<TKey, TValue>[] | array | The array to copy the entries into. |
System.Int32 | arrayIndex | The index of the array at which to start copying values. |
Implements
ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue>)
Removes the specified key/value pair from the map.
Declaration
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.KeyValuePair<TKey, TValue> | item | The key/value pair to remove. |
Returns
Type | Description |
---|---|
System.Boolean |
|
Implements
Remarks
Both the key and the value must be found for the entry to be removed.
IReadOnlyDictionary<TKey, TValue>.Keys
Declaration
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys { get; }
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<TKey> |
Implements
IReadOnlyDictionary<TKey, TValue>.Values
Declaration
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values { get; }
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<TValue> |
Implements
ICollection.CopyTo(Array, Int32)
Declaration
void ICollection.CopyTo(Array array, int index)
Parameters
Type | Name | Description |
---|---|---|
System.Array | array | |
System.Int32 | index |
Implements
ICollection.IsSynchronized
Declaration
bool ICollection.IsSynchronized { get; }
Returns
Type | Description |
---|---|
System.Boolean |
Implements
ICollection.SyncRoot
Declaration
object ICollection.SyncRoot { get; }
Returns
Type | Description |
---|---|
System.Object |
Implements
IDictionary.Add(Object, Object)
Declaration
void IDictionary.Add(object key, object value)
Parameters
Type | Name | Description |
---|---|---|
System.Object | key | |
System.Object | value |
Implements
IDictionary.Contains(Object)
Declaration
bool IDictionary.Contains(object key)
Parameters
Type | Name | Description |
---|---|---|
System.Object | key |
Returns
Type | Description |
---|---|
System.Boolean |
Implements
IDictionary.GetEnumerator()
Declaration
IDictionaryEnumerator IDictionary.GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.IDictionaryEnumerator |
Implements
IDictionary.IsFixedSize
Declaration
bool IDictionary.IsFixedSize { get; }
Returns
Type | Description |
---|---|
System.Boolean |
Implements
IDictionary.Item[Object]
Declaration
object IDictionary.this[object key] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
System.Object | key |
Returns
Type | Description |
---|---|
System.Object |
Implements
IDictionary.Keys
Declaration
ICollection IDictionary.Keys { get; }
Returns
Type | Description |
---|---|
System.Collections.ICollection |
Implements
IDictionary.Remove(Object)
Declaration
void IDictionary.Remove(object key)
Parameters
Type | Name | Description |
---|---|---|
System.Object | key |
Implements
IDictionary.Values
Declaration
ICollection IDictionary.Values { get; }
Returns
Type | Description |
---|---|
System.Collections.ICollection |
Implements
IEnumerable.GetEnumerator()
Returns an enumerator that iterates through a collection.
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.IEnumerator | An System.Collections.IEnumerator object that can be used to iterate through the collection. |