Class MapField<TKey, TValue> (3.23.0)

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

NameDescription
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
TypeDescription
int

IsReadOnly

public bool IsReadOnly { get; }

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

Property Value
TypeDescription
bool

this[TKey]

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

Gets or sets the value associated with the specified key.

Parameter
NameDescription
keyTKey

The key of the value to get or set.

Property Value
TypeDescription
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
TypeDescription
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
TypeDescription
ICollection

Values

public ICollection<TValue> Values { get; }

Gets a collection containing the values in the map.

Property Value
TypeDescription
ICollection

Methods

Add(TKey, TValue)

public void Add(TKey key, TValue value)

Adds the specified key/value pair to the map.

Parameters
NameDescription
keyTKey

The key to add

valueTValue

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
TypeDescription
ArgumentException

The given key already exists in map.

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
NameDescription
entriesIDictionary

The entries to add to the map.

AddEntriesFrom(CodedInputStream, Codec)

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

Adds entries to the map from the given stream.

Parameters
NameDescription
inputCodedInputStream

Stream to read from

codecMapFieldCodec

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
NameDescription
ctxParseContext

Input to read from

codecMapFieldCodec

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
NameDescription
codecMapFieldCodec

The codec to use to encode each entry.

Returns
TypeDescription
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
TypeDescription
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
NameDescription
keyTKey

The key to check.

Returns
TypeDescription
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
NameDescription
otherMapField

The map to compare this with.

Returns
TypeDescription
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
NameDescription
otherobject

The object to compare with this instance.

Returns
TypeDescription
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
TypeDescription
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
TypeDescription
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
NameDescription
entriesIDictionary

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
NameDescription
keyTKey

The key indicating the entry to remove from the map.

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

TryGetValue(TKey, out TValue)

public bool TryGetValue(TKey key, out TValue value)

Gets the value associated with the specified key.

Parameters
NameDescription
keyTKey

The key whose value to get.

valueTValue

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
TypeDescription
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
NameDescription
outputCodedOutputStream

The output stream to write to.

codecMapFieldCodec

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
NameDescription
ctxWriteContext

The write context to write to.

codecMapFieldCodec

The codec to use for each entry.