Skip to content

[API Proposal]: Add extension method AsReadOnly() for IDictionary<TKey, TValue> #33033

Closed

Description

@Mrxx99

Background and motivation

Like the List<T> has a method AsReadOnly() I would like to propose the same for the Dictionary.
This could lead to more clean and fluent code, when you can call AsReadOnly at the end of a call chain instead of creating a new instance of ReadOnlyDictionary manually.

I am aware that Dictionary already implements IReadOnlyDictionary but only using the ReadOnlyDictionary it is really immutable without the possibility to cast it back to (I)Dictionary.

API Proposal

namespace System.Collections.Generic
{
    public static class CollectionExtensions
    {
+       public static ReadOnlyDictionary<TKey, TValue> AsReadOnly(this IDictionary<TKey, TValue> dictionary)
+            => new ReadOnlyDictionary<TKey, TValue>(dictionary);
    }
}

API Usage

private Dictionary<string, string> _theDictionary = new Dictionary<string, string>();

public IReadOnlyDictionary<string, string> TheDictionary => _theDictionary.AsReadOnly();

Alternative Designs

The origianl proposal (as can be read in the comments, an extension method is preferred):

public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, IDictionary...
{
+        public ReadOnlyDictionary<TKey, TValue> AsReadOnly()
+            => new ReadOnlyDictionary<TKey, TValue>(this);
}

Risks

I don't see any it's just one extension method more calling a very common constructor.

Activity

  1. DevDrake commented on Mar 3, 2020

    @DevDrake

    I was scratching my head thinking about this proposal.
    If we need the functionality i would be more opt to extension function or default interface implementation for IDictionary<,>
    public ReadOnlyDictionary<TKey, TValue> AsReadOnly(this IDictionary<TKey, TValue> dictionary) => new ReadOnlyDictionary<TKey, TValue>(dictionary)
    as this will cover more than just Dictionary.
    also this is already used in runtime/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/MetadataServices.cs for example.
    I'm not very convinced we need it at all.
    IMHO we should go more in the #31573 direction

  2. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Jun 24, 2020
  3. added this to the Future milestone on Jun 24, 2020
  4. removed
    untriagedNew issue has not been triaged by the area owner
    on Jun 24, 2020
  5. eiriktsarpalis commented on Nov 24, 2020

    @eiriktsarpalis
    Member

    We'd probably want to express this as an extension method due to codegen size concerns. How about this?

    namespace System.Collections.Generic
    {
        public static class CollectionExtensions
        {
    +       public static ReadOnlyDictionary<TKey, TValue> AsReadOnly(this IDictionary<TKey, TValue> dictionary)
    +            => new ReadOnlyDictionary<TKey, TValue>(dictionary);
        }
    }

    cc @jkotas @GrabYourPitchforks

  6. Mrxx99 commented on Nov 24, 2020

    @Mrxx99
    ContributorAuthor

    I agree, I think it does make more sense as extension method.

  7. mattjohnsonpint commented on Jun 18, 2021

    @mattjohnsonpint
    Contributor

    Is this API still up for consideration?

  8. eiriktsarpalis commented on Jun 28, 2021

    @eiriktsarpalis
    Member

    It is, but we're not prioritizing it for .NET 6.

  9. modified the milestones: Future, 7.0.0 on Oct 29, 2021
  10. eiriktsarpalis commented on Oct 29, 2021

    @eiriktsarpalis
    Member

    @Mrxx99 would it be possible to update your API proposal following the API proposal template? Please also change the API diff so that it's expressed as an extension method as #33033 (comment)

  11. changed the title [-]Add Dictionary<T>.AsReadOnly()[/-] [+][API Proposal]: Add Dictionary<T>.AsReadOnly()[/+] on Oct 29, 2021
  12. 5 remaining items

  13. terrajobst commented on Nov 2, 2021

    @terrajobst
    Contributor

    Video

    • This makes sense, but it seems we should be consistent and offer one for ReadOnlyCollection<T>, which today only exists as instance methods on List<T> and Array.
    namespace System.Collections.Generic
    {
        public static class CollectionExtensions
        {
            public static ReadOnlyCollection<T> AsReadOnly<T>(this IList<T> list);
            public static ReadOnlyDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(this IDictionary<TKey, TValue> dictionary);
        }
    }
  14. added
    api-approvedAPI was approved in API review, it can be implemented
    and removed
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    on Nov 2, 2021
  15. eiriktsarpalis commented on Nov 2, 2021

    @eiriktsarpalis
    Member

    @Mrxx99 would you be interested in providing an implementation?

  16. Mrxx99 commented on Nov 2, 2021

    @Mrxx99
    ContributorAuthor

    @eiriktsarpalis Yes, I would be interested. Also thanks for moving this issue forward. Should I also update the proposal to also include the extension method on IList?

  17. eiriktsarpalis commented on Nov 2, 2021

    @eiriktsarpalis
    Member

    Should I also update the proposal to also include the extension method on IList?

    No that's fine, we use #33033 (comment) as the source of truth.

  18. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Nov 3, 2021
  19. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Nov 23, 2021
  20. ghost locked as resolved and limited conversation to collaborators on Dec 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedapi-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Collectionshelp wanted[up-for-grabs] Good issue for external contributors

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions