Class MapField<TKey, TValue> (3.27.1)

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 > MapField<TKey, TValue>

Namespace

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.

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

public int Count { get; }

Gets the number of elements contained in the map.

Property Value
Type Description
int

IsReadOnly

public bool IsReadOnly { get; }

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

Property Value
Type Description
bool

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, and a set operation creates a new element with the specified key.

Exceptions
Type Description
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
ICollection

Values

public ICollection<TValue> Values { get; }

Gets a collection containing the values in the map.

Property Value
Type Description
ICollection

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 IDictionary

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
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 CodedInputStream

Stream to read from

codec MapFieldCodec

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 ParseContext

Input to read from

codec MapFieldCodec

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 MapFieldCodec

The codec to use to encode each entry.

Returns
Type Description
int

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
MapField

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
bool

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 MapField

The map to compare this with.

Returns
Type Description
bool

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, is equal to this instance.

Parameter
Name Description
other object

The object to compare with this instance.

Returns
Type Description
bool

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

Overrides

GetEnumerator()

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

Returns an enumerator that iterates through the collection.

Returns
Type Description
IEnumeratorKeyValuePair

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
int

A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.

Overrides

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 IDictionary

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
bool

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
string
Overrides

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
bool

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 CodedOutputStream

The output stream to write to.

codec MapFieldCodec

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 WriteContext

The write context to write to.

codec MapFieldCodec

The codec to use for each entry.