Skip to content
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

J2N.Collections: Added AsReadOnly() method to each dictionary and set #100

Merged
merged 1 commit into from
Aug 19, 2024

Conversation

NightOwl888
Copy link
Owner

@NightOwl888 NightOwl888 commented Aug 19, 2024

Closes #93.

J2N.Collections: Added AsReadOnly() method to each dictionary and set to ensure it is unaffected by Micorsoft's extension methods of the same name in net core. This ensures read-only collections of the built-in types will always respect structural equality and structural formatting.

There will still be collisions using .AsReadOnly() if declaring using J2N.Collections.Generic.Extensions; and using System.Collections.Generic;. In those cases, the user must call the static method that they prefer explicitly.

Our extension method existed prior to Microsoft's. We followed a best practice of putting it into a separate namespace than J2N.Collections.Generic so users would have an option to not import the J2N.Collections.Generic.Extensions if there were a conflict with another library. It was assumed that Microsoft would declare AsReadOnly() on each collection (as it is on List<T>) rather than using an extension method so there would be no conflict. Or at least that they would follow the best practice of not including it directly in the System.Collections.Generic namespace. Unfortunately, they did neither of those things.

New APIs:

namespace J2N.Collections.Concurrent
{
    public class LurchTable<TKey, TValue>
    {
        public ReadOnlyDictionary<TKey, TValue> AsReadOnly(); 
    }
}

namespace J2N.Collections.Generic
{
    public class Dictionary<TKey, TValue>
    {
        public ReadOnlyDictionary<TKey, TValue> AsReadOnly();
    }
    public class HashSet<T>
    {
        public ReadOnlySet<T> AsReadOnly();
    }
    public class LinkedDictionary<TKey, TValue>
    {
        public ReadOnlyDictionary<TKey, TValue> AsReadOnly();
    }
    public class LinkedHashSet<T>
    {
        public ReadOnlySet<T> AsReadOnly();
    }
    public class SortedDictionary<TKey, TValue>
    {
        public ReadOnlyDictionary<TKey, TValue> AsReadOnly();
    }
    public class SortedSet<T>
    {
        public ReadOnlySet<T> AsReadOnly();
    }
}

… to ensure it is unaffected by Micorsoft's extension methods of the same name in net core.
@NightOwl888 NightOwl888 added this to the 2.1 milestone Aug 19, 2024
@NightOwl888 NightOwl888 merged commit dae4c56 into main Aug 19, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Task: Fix conflicts with AsReadOnly() extensions
1 participant