Class MapField<TKey, TValue> (3.15.8)

public sealed class MapField<TKey, TValue> : object, IDeepCloneable<MapField<TKey, TValue>>, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary, IReadOnlyDictionary<TKey, TValue>

Representation of a map field in a Protocol Buffer message.

Inheritance

Object > MapField<TKey, TValue>

Implements

IDeepCloneable<MapField<TKey, TValue>>, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary, IReadOnlyDictionary<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 .

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
Int32

IsReadOnly

public bool IsReadOnly { get; }

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

Property Value
TypeDescription
Boolean

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

Keys

public ICollection<TKey> Keys { get; }

Gets a collection containing the keys in the map.

Property Value
TypeDescription
ICollection<TKey>

Values

public ICollection<TValue> Values { get; }

Gets a collection containing the values in the map.

Property Value
TypeDescription
ICollection<TValue>

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.

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

The entries to add to the map.

AddEntriesFrom(CodedInputStream, MapField<TKey, TValue>.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

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

codecMapField.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(MapField<TKey, TValue>.Codec)

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

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

Parameter
NameDescription
codecMapField.Codec<>

The codec to use to encode each entry.

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

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
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
NameDescription
otherMapField<TKey, TValue>

The map to compare this with.

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

Parameter
NameDescription
otherObject

The to compare with this instance.

Returns
TypeDescription
Boolean

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

GetEnumerator()

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

Returns an enumerator that iterates through the collection.

Returns
TypeDescription
IEnumerator<KeyValuePair<TKey, TValue>>

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
Int32

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

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

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
Boolean

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

WriteTo(CodedOutputStream, MapField<TKey, TValue>.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.

codecMapField.Codec<>

The codec to use for each entry.

WriteTo(ref WriteContext, MapField<TKey, TValue>.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.

codecMapField.Codec<>

The codec to use for each entry.