-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Add extension method AsReadOnly() for IDictionary<TKey, TValue> #33033
Comments
I was scratching my head thinking about this proposal. |
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);
}
} |
I agree, I think it does make more sense as extension method. |
Is this API still up for consideration? |
It is, but we're not prioritizing it for .NET 6. |
@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) |
@eiriktsarpalis done. |
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);
}
} |
@Mrxx99 would you be interested in providing an implementation? |
@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? |
No that's fine, we use #33033 (comment) as the source of truth. |
Background and motivation
Like the
List<T>
has a methodAsReadOnly()
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
API Usage
Alternative Designs
The origianl proposal (as can be read in the comments, an extension method is preferred):
Risks
I don't see any it's just one extension method more calling a very common constructor.
The text was updated successfully, but these errors were encountered: