You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[API Proposal]: Add extension method AsReadOnly() for IDictionary<TKey, TValue> #33033
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);
}
}
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.
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
@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)
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.
@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?
Activity
DevDrake commentedon Mar 3, 2020
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
eiriktsarpalis commentedon Nov 24, 2020
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
Mrxx99 commentedon Nov 24, 2020
I agree, I think it does make more sense as extension method.
mattjohnsonpint commentedon Jun 18, 2021
Is this API still up for consideration?
eiriktsarpalis commentedon Jun 28, 2021
It is, but we're not prioritizing it for .NET 6.
eiriktsarpalis commentedon Oct 29, 2021
@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)
[-]Add Dictionary<T>.AsReadOnly()[/-][+][API Proposal]: Add Dictionary<T>.AsReadOnly()[/+]5 remaining items
terrajobst commentedon Nov 2, 2021
Video
ReadOnlyCollection<T>, which today only exists as instance methods onList<T>andArray.eiriktsarpalis commentedon Nov 2, 2021
@Mrxx99 would you be interested in providing an implementation?
Mrxx99 commentedon Nov 2, 2021
@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?
eiriktsarpalis commentedon Nov 2, 2021
No that's fine, we use #33033 (comment) as the source of truth.