dotnet / dotnet/runtime

[API Proposal]: Add a generic ReadOnlyOrderedDictionary class

Open
#121,424 2 comments 1 reaction 0 assignees View on GitHub
api-suggestion area-System.Collections
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Background and motivation

We now have a generic `OrderedDictionary` and a `ReadOnlyDictionary` but no combination of the two. We have need of such a data structure where the key order is important and we also want it to be immutable.

### API Proposal

```csharp
namespace System.Collections.Generic;

public class ReadOnlyOrderedDictionary :
IDictionary, IReadOnlyDictionary, IDictionary,
IList>, IReadOnlyList>, IList
where TKey : not null
{
// Same methods/properties as ReadOnlyDictionary, but enumerations are ordered
}
```

### API Usage

```csharp
var dict = new ReadOnlyOrderedDictionary(
[
new KeyValuePair("foo","bar"),
new KeyValuePair("foo1","bar2"),
]);

foreach ((var key, var value) in dict)
{
// ...
}
```

### Alternative Designs

_No response_

### Risks

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.