Show / Hide Table of Contents

Class MapField<TKey, TValue>

Representation of a map field in a Protocol Buffer message.

Inheritance
System.Object
MapField<TKey, TValue>
Implements
IDeepCloneable<MapField<TKey, TValue>>
System.Collections.Generic.IDictionary<TKey, TValue>
System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>
System.IEquatable<MapField<TKey, TValue>>
System.Collections.IDictionary
System.Collections.ICollection
System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>
System.Collections.Generic.IReadOnlyCollection<System.Collections.Generic.KeyValuePair<TKey, TValue>>
System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey, TValue>>
System.Collections.IEnumerable
Inherited Members
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetType()
System.Object.MemberwiseClone()
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
System.Collections.Generic.ICollection<T>.Count
System.Collections.ICollection.Count
System.Collections.Generic.IReadOnlyCollection<T>.Count

IsReadOnly

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

Declaration
public bool IsReadOnly { get; }
Property Value
Type Description
System.Boolean
Implements
System.Collections.Generic.ICollection<T>.IsReadOnly
System.Collections.IDictionary.IsReadOnly

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
System.Collections.Generic.IDictionary<TKey, TValue>.Item[TKey]
System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>.Item[TKey]
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
System.Collections.Generic.IDictionary<TKey, TValue>.Keys

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
System.Collections.Generic.IDictionary<TKey, TValue>.Values

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
System.Collections.Generic.IDictionary<TKey, TValue>.Add(TKey, TValue)
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
System.Collections.Generic.ICollection<T>.Clear()
System.Collections.IDictionary.Clear()

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
IDeepCloneable<T>.Clone()

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

true if the map contains the given key; false otherwise.

Implements
System.Collections.Generic.IDictionary<TKey, TValue>.ContainsKey(TKey)
System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>.ContainsKey(TKey)

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

true if other refers to an equal map; false otherwise.

Implements
System.IEquatable<T>.Equals(T)
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

true if the specified System.Object is equal to this instance; otherwise, false.

Overrides
System.Object.Equals(System.Object)

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
System.Collections.Generic.IEnumerable<T>.GetEnumerator()

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
System.Object.GetHashCode()

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

true if the map contained the given key before the entry was removed; false otherwise.

Implements
System.Collections.Generic.IDictionary<TKey, TValue>.Remove(TKey)

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
System.Object.ToString()

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 value parameter. This parameter is passed uninitialized.

Returns
Type Description
System.Boolean

true if the map contains an element with the specified key; otherwise, false.

Implements
System.Collections.Generic.IDictionary<TKey, TValue>.TryGetValue(TKey, TValue)
System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>.TryGetValue(TKey, TValue)

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
System.Collections.Generic.ICollection<T>.Add(T)

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
System.Collections.Generic.ICollection<T>.Contains(T)

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
System.Collections.Generic.ICollection<T>.CopyTo(T[], System.Int32)

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

true if the key/value pair was found and removed; false otherwise.

Implements
System.Collections.Generic.ICollection<T>.Remove(T)
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
System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>.Keys

IReadOnlyDictionary<TKey, TValue>.Values

Declaration
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values { get; }
Returns
Type Description
System.Collections.Generic.IEnumerable<TValue>
Implements
System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>.Values

ICollection.CopyTo(Array, Int32)

Declaration
void ICollection.CopyTo(Array array, int index)
Parameters
Type Name Description
System.Array array
System.Int32 index
Implements
System.Collections.ICollection.CopyTo(System.Array, System.Int32)

ICollection.IsSynchronized

Declaration
bool ICollection.IsSynchronized { get; }
Returns
Type Description
System.Boolean
Implements
System.Collections.ICollection.IsSynchronized

ICollection.SyncRoot

Declaration
object ICollection.SyncRoot { get; }
Returns
Type Description
System.Object
Implements
System.Collections.ICollection.SyncRoot

IDictionary.Add(Object, Object)

Declaration
void IDictionary.Add(object key, object value)
Parameters
Type Name Description
System.Object key
System.Object value
Implements
System.Collections.IDictionary.Add(System.Object, System.Object)

IDictionary.Contains(Object)

Declaration
bool IDictionary.Contains(object key)
Parameters
Type Name Description
System.Object key
Returns
Type Description
System.Boolean
Implements
System.Collections.IDictionary.Contains(System.Object)

IDictionary.GetEnumerator()

Declaration
IDictionaryEnumerator IDictionary.GetEnumerator()
Returns
Type Description
System.Collections.IDictionaryEnumerator
Implements
System.Collections.IDictionary.GetEnumerator()

IDictionary.IsFixedSize

Declaration
bool IDictionary.IsFixedSize { get; }
Returns
Type Description
System.Boolean
Implements
System.Collections.IDictionary.IsFixedSize

IDictionary.Item[Object]

Declaration
object IDictionary.this[object key] { get; set; }
Parameters
Type Name Description
System.Object key
Returns
Type Description
System.Object
Implements
System.Collections.IDictionary.Item[System.Object]

IDictionary.Keys

Declaration
ICollection IDictionary.Keys { get; }
Returns
Type Description
System.Collections.ICollection
Implements
System.Collections.IDictionary.Keys

IDictionary.Remove(Object)

Declaration
void IDictionary.Remove(object key)
Parameters
Type Name Description
System.Object key
Implements
System.Collections.IDictionary.Remove(System.Object)

IDictionary.Values

Declaration
ICollection IDictionary.Values { get; }
Returns
Type Description
System.Collections.ICollection
Implements
System.Collections.IDictionary.Values

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.

Implements
System.Collections.IEnumerable.GetEnumerator()
Back to top