# Class MapField&lt;TKey, TValue&gt; (3.27.1)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [3.27.1 (latest)](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2)
- [3.23.0](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.23.0/Google.Protobuf.Collections.MapField-2)
- [3.15.8](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/3.15.8/Google.Protobuf.Collections.MapField-2)

    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

Representation of a map field in a Protocol Buffer message.

## Inheritance

[object](https://learn.microsoft.com/dotnet/api/system.object) \> MapField\<TKey, TValue\>

## Implements

[IDeepCloneable](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.IDeepCloneable-1)[MapField](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2), [IDictionary](https://learn.microsoft.com/dotnet/api/system.collections.generic.idictionary-2), [ICollection](https://learn.microsoft.com/dotnet/api/system.collections.generic.icollection-1)[KeyValuePair](https://learn.microsoft.com/dotnet/api/system.collections.generic.keyvaluepair-2), [IEquatable](https://learn.microsoft.com/dotnet/api/system.iequatable-1)[MapField](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2), [IDictionary](https://learn.microsoft.com/dotnet/api/system.collections.idictionary), [ICollection](https://learn.microsoft.com/dotnet/api/system.collections.icollection), [IReadOnlyDictionary](https://learn.microsoft.com/dotnet/api/system.collections.generic.ireadonlydictionary-2), [IReadOnlyCollection](https://learn.microsoft.com/dotnet/api/system.collections.generic.ireadonlycollection-1)[KeyValuePair](https://learn.microsoft.com/dotnet/api/system.collections.generic.keyvaluepair-2), [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerable-1)[KeyValuePair](https://learn.microsoft.com/dotnet/api/system.collections.generic.keyvaluepair-2), [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.ienumerable)

## Inherited Members

[object.Equals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object-system-object)) [object.GetType()](https://learn.microsoft.com/dotnet/api/system.object.gettype) [object.ReferenceEquals(object, object)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals)

## Namespace

[Google.Protobuf.Collections](https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections)

## Assembly

Google.Protobuf.dll

| ## 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 [Ordinal](https://learn.microsoft.com/dotnet/api/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.

<br />


The order in which entries are returned when iterating over this object is undefined, and may change
in future versions.

## Properties

### Count

    public int Count { get; }

Gets the number of elements contained in the map.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` |   |

### IsReadOnly

    public bool IsReadOnly { get; }

Gets a value indicating whether the map is read-only.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` |   |

### this\[TKey\]

    public TValue this[TKey key] { get; set; }

Gets or sets the value associated with the specified key.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `key` | `TKey` 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 [KeyNotFoundException](https://learn.microsoft.com/dotnet/api/system.collections.generic.keynotfoundexception), and a set operation creates a new element with the specified key. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.collections.generic.keynotfoundexception` | The property is retrieved and key does not exist in the collection. |

### Keys

    public ICollection<TKey> Keys { get; }

Gets a collection containing the keys in the map.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.collections.generic.icollection-1` |   |

### Values

    public ICollection<TValue> Values { get; }

Gets a collection containing the values in the map.

| **Property Value** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.collections.generic.icollection-1` |   |

## Methods

### Add(IDictionary\<TKey, TValue\>)

    public void Add(IDictionary<TKey, TValue> entries)

Adds the specified entries to the map. The keys and values are not automatically cloned.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `entries` | `https://learn.microsoft.com/dotnet/api/system.collections.generic.idictionary-2` The entries to add to the map. |

### Add(TKey, TValue)

    public void Add(TKey key, TValue value)

Adds the specified key/value pair to the map.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `key` | `TKey` The key to add |
| `value` | `TValue` The value to add. |

**Remarks** This operation fails if the key already exists in the map. To replace an existing entry, use the indexer.

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.argumentexception` | The given key already exists in map. |

### AddEntriesFrom(CodedInputStream, Codec)

    public void AddEntriesFrom(CodedInputStream input, MapField<TKey, TValue>.Codec codec)

Adds entries to the map from the given stream.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `input` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedInputStream` Stream to read from |
| `codec` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2.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.

### AddEntriesFrom(ref ParseContext, Codec)

    public void AddEntriesFrom(ref ParseContext ctx, MapField<TKey, TValue>.Codec codec)

Adds entries to the map from the given parse context.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `ctx` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.ParseContext` Input to read from |
| `codec` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2.Codec` Codec describing how the key/value pairs are encoded |

**Remarks** It is assumed that the input is initially positioned after the tag specified by the codec.
This method will continue reading entries from the input until the end is reached, or
a different tag is encountered.

### CalculateSize(Codec)

    public int CalculateSize(MapField<TKey, TValue>.Codec codec)

Calculates the size of this map based on the given entry codec.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `codec` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2.Codec` The codec to use to encode each entry. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` |   |

### Clear()

    public void Clear()

Removes all items from the map.

### Clone()

    public MapField<TKey, TValue> Clone()

Creates a deep clone of this object.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2` | A deep clone of this object. |

### ContainsKey(TKey)

    public bool ContainsKey(TKey key)

Determines whether the specified key is present in the map.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `key` | `TKey` The key to check. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` | `true` if the map contains the given key; `false` otherwise. |

### Equals(MapField\<TKey, TValue\>)

    public bool Equals(MapField<TKey, TValue> other)

Compares this map with another for equality.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `other` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2` The map to compare this with. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` | `true` if `other` refers to an equal map; `false` otherwise. |

**Remarks** The order of the key/value pairs in the maps is not deemed significant in this comparison.

### Equals(object)

    public override bool Equals(object other)

Determines whether the specified [object](https://learn.microsoft.com/dotnet/api/system.object), is equal to this instance.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `other` | `https://learn.microsoft.com/dotnet/api/system.object` The [object](https://learn.microsoft.com/dotnet/api/system.object) to compare with this instance. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` | `true` if the specified [object](https://learn.microsoft.com/dotnet/api/system.object) is equal to this instance; otherwise, `false`. |

**Overrides** [object.Equals(object)](https://learn.microsoft.com/dotnet/api/system.object.equals#system-object-equals(system-object))

### GetEnumerator()

    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()

Returns an enumerator that iterates through the collection.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.collections.generic.ienumerator-1https://learn.microsoft.com/dotnet/api/system.collections.generic.keyvaluepair-2` | An enumerator that can be used to iterate through the collection. |

### GetHashCode()

    public override int GetHashCode()

Returns a hash code for this instance.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.int32` | A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. |

**Overrides** [object.GetHashCode()](https://learn.microsoft.com/dotnet/api/system.object.gethashcode)

### MergeFrom(IDictionary\<TKey, TValue\>)

    public void MergeFrom(IDictionary<TKey, TValue> entries)

Adds the specified entries to the map, replacing any existing entries with the same keys.
The keys and values are not automatically cloned.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `entries` | `https://learn.microsoft.com/dotnet/api/system.collections.generic.idictionary-2` The entries to add to the map. |

**Remarks** This method primarily exists to be called from MergeFrom methods in generated classes for messages.

### Remove(TKey)

    public bool Remove(TKey key)

Removes the entry identified by the given key from the map.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `key` | `TKey` The key indicating the entry to remove from the map. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` | `true` if the map contained the given key before the entry was removed; `false` otherwise. |

### ToString()

    public override string ToString()

Returns a string representation of this repeated field, in the same
way as it would be represented by the default JSON formatter.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.string` |   |

**Overrides** [object.ToString()](https://learn.microsoft.com/dotnet/api/system.object.tostring)

### TryGetValue(TKey, out TValue)

    public bool TryGetValue(TKey key, out TValue value)

Gets the value associated with the specified key.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `key` | `TKey` The key whose value to get. |
| `value` | `TValue` 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 `value` parameter. This parameter is passed uninitialized. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://learn.microsoft.com/dotnet/api/system.boolean` | `true` if the map contains an element with the specified key; otherwise, `false`. |

### WriteTo(CodedOutputStream, Codec)

    public void WriteTo(CodedOutputStream output, MapField<TKey, TValue>.Codec codec)

Writes the contents of this map to the given coded output stream, using the specified codec
to encode each entry.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `output` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.CodedOutputStream` The output stream to write to. |
| `codec` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2.Codec` The codec to use for each entry. |

### WriteTo(ref WriteContext, Codec)

    public void WriteTo(ref WriteContext ctx, MapField<TKey, TValue>.Codec codec)

Writes the contents of this map to the given write context, using the specified codec
to encode each entry.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `ctx` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.WriteContext` The write context to write to. |
| `codec` | `https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2https://docs.cloud.google.com/dotnet/docs/reference/Google.Protobuf/latest/Google.Protobuf.Collections.MapField-2.Codec` The codec to use for each entry. |