Table of Contents

Class NameValueRecordCollection<TKey, TValue>

Namespace
CSF.Screenplay.WebApis
Assembly
CSF.Screenplay.WebApis.dll

A simple name/value collection with an indexer, backed by a Dictionary<TKey, TValue>.

public sealed class NameValueRecordCollection<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IEquatable<NameValueRecordCollection<TKey, TValue>> where TKey : notnull where TValue : class

Type Parameters

TKey

The key type

TValue

The value type

Inheritance
NameValueRecordCollection<TKey, TValue>
Implements
IEnumerable<KeyValuePair<TKey, TValue>>
Inherited Members

Remarks

Please note that this type differs in form depending upon the .NET version under which it is consumed. If consuming this type from logic which targets .NET Standard 2.0 or .NET Framework 4.6.2 then this type is mutable. In that scenario the developer should take care to ensure that they do not mutate/alter its state inadvertently. Developers should ensure that they manually copy the state from the current instance into a new instance instead of modifying an existing instance. In these target frameworks, a Clone method has been provided to assist with this.

When consuming this from .NET 5 or higher, this type is immutable by design; the indexer is init-only. When using .NET 5, developers may use nondestructive mutation by using either the WithItem or WithItems methods to create a copy of the current instance, but with modified items.

Constructors

NameValueRecordCollection()

Initialises a new instance of NameValueRecordCollection<TKey, TValue>.

public NameValueRecordCollection()

Properties

this[TKey]

Gets or sets the values within this collection, via an indexer.

public TValue? this[TKey key] { get; init; }

Parameters

key TKey

The key

Property Value

TValue

The value

Examples

In order to set this property, particularly when using .NET 5 or higher (when this type is a record rather than a class, and this indexer is immutable), use the following syntax.

var nvr = new NameValueRecordCollection<int,string>
{
    [7] = "seven",
    [9] = "nine",
    [13] = "thirteen",
}

Remarks

null may not be stored in this collection as a value. An attempt to store a null value will result in the removal of the item at the specified key.

As a type which is intended to be immutable, it goes without saying that ideally both TKey and TValue should ideally also be immutable types. If they are not, developers must be careful to avoid mutating them, as doing so could adulterate the state of the current instance.

Methods

Equals(NameValueRecordCollection<TKey, TValue>?)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(NameValueRecordCollection<TKey, TValue>? other)

Parameters

other NameValueRecordCollection<TKey, TValue>

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

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

GetEnumerator()

Returns an enumerator that iterates through the collection.

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

Returns

IEnumerator<KeyValuePair<TKey, TValue>>

An enumerator that can be used to iterate through the collection.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

WithItem(TKey, TValue?)

Gets a clone (shallow copy) of the current NameValueRecordCollection<TKey, TValue> containing the same items, as well as the specified item.

public NameValueRecordCollection<TKey, TValue> WithItem(TKey key, TValue? value)

Parameters

key TKey

The key at which to add, remove or update an item

value TValue

The value to store for the item, or null which indicates that the item is to be removed.

Returns

NameValueRecordCollection<TKey, TValue>

A copy of the current instance, with a single item added, removed or altered in that copied instance.

Remarks

As with the indexer, if the value is null then this will result in the removal of the item in the returned copy.

WithItems(IEnumerable<KeyValuePair<TKey, TValue?>>)

Gets a clone (shallow copy) of the current NameValueRecordCollection<TKey, TValue> containing the same items, as well as the specified items.

public NameValueRecordCollection<TKey, TValue> WithItems(IEnumerable<KeyValuePair<TKey, TValue?>> items)

Parameters

items IEnumerable<KeyValuePair<TKey, TValue>>

A collection of key/value pairs, indicating the keys & values to add, remove or alter in the copied instance.

Returns

NameValueRecordCollection<TKey, TValue>

A copy of the current instance, with the specified items added, removed or altered.

Remarks

As with indexer, if any value is null then this will result in the removal of the corresponding item in the returned copy.