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

Access SortedList<Tkey, TValue> by index #26591

Closed
ghost opened this issue Jun 24, 2018 · 18 comments
Closed

Access SortedList<Tkey, TValue> by index #26591

ghost opened this issue Jun 24, 2018 · 18 comments
Labels
area-System.Collections question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@ghost
Copy link

ghost commented Jun 24, 2018

SortedList<Tkey, TValue> has these two methods:
1- IndexOfKey
2- IndexOfValue
They return the index of the item in the sorted list, but there is no way to access any item by its index! The only method that uses the index is RemoveAt!
So, why there is no Indexer that accepts the index? This class is a LIST not a Dictionary, and lists are accessible by index.

@jkotas jkotas closed this as completed Jun 24, 2018
@ghost
Copy link

ghost commented Jun 24, 2018

@jkotas
Thanks. I was using .net Framework 4.7.2 (thinking it has the same API as .net core) but there is no such methods!.. This brings the question, why? They exist since .net core 1.0.

@jkotas
Copy link
Member

jkotas commented Jun 24, 2018

These methods do exist in .NET Framework 4.7.2. The doc has a drop down at the left side that lets you see all versions where the method is available.

@ghost
Copy link

ghost commented Jun 24, 2018

The end of doc sias:

Applies to
.NET Core
2.1, 2.0, 1.1, 1.0
.NET Framework
4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5

@ghost
Copy link

ghost commented Jun 24, 2018

Obviously this is not true, and I ask why, since MS always says that every thing added to .NET core, will eventually appear in .NET Framework ?

@ghost
Copy link

ghost commented Jun 24, 2018

@jkotas
Is there a Nuget for Collections? Do the docs say the NuGet of each class on a namespace?

@jkotas
Copy link
Member

jkotas commented Jun 24, 2018

Obviously this is not true

I have double check. These method are in .NET Framework 4.7.2. What makes you think that they are not there?

MS always says that every thing added to .NET core, will eventually appear in .NET Framework

This is not a correct statement. It is not what we have been saying for a while. Where did you hear this?

@jkotas
Copy link
Member

jkotas commented Jun 24, 2018

Is there a Nuget for Collections? Do the docs say the NuGet of each class on a namespace?

SortedList is part of the core framework. You do not need to reference any NuGet to get it - there is not any.

@ghost
Copy link

ghost commented Jun 24, 2018

I try this in a C# windows forms framework 4.7.2 project:

            var s = new SortedList<string, int>() { { "x", 1 } };
            var k = s.GetKey(0);

and C# doesn't find the method GetKey!

@jkotas
Copy link
Member

jkotas commented Jun 24, 2018

Do you really have framework 4.7.2 selected as you target framework? Right click on the project and select properties.

image

@ghost
Copy link

ghost commented Jun 24, 2018

@jkotas
Yes, this is the first thing I checked.
the metadata code page for SortedList class shows this:

namespace System.Collections.Generic
{
    public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IDictionary, ICollection, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>
    {
        public SortedList();
        public SortedList(int capacity);
        public SortedList(IComparer<TKey> comparer);
        public SortedList(IDictionary<TKey, TValue> dictionary);
        public SortedList(int capacity, IComparer<TKey> comparer);
        public SortedList(IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer);
        public TValue this[TKey key] { get; set; }
        public IList<TValue> Values { get; }
        public int Capacity { get; set; }
        public IComparer<TKey> Comparer { get; }
        public int Count { get; }
        public IList<TKey> Keys { get; }

        public void Add(TKey key, TValue value);
        public void Clear();
        public bool ContainsKey(TKey key);
        public bool ContainsValue(TValue value);
        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator();
        public int IndexOfKey(TKey key);
        public int IndexOfValue(TValue value);
        public bool Remove(TKey key);
        public void RemoveAt(int index);
        public void TrimExcess();
        public bool TryGetValue(TKey key, out TValue value);
    }

@ghost
Copy link

ghost commented Jun 24, 2018

@jkotas
I even tried to use the extension method ToHashSet and it is there. It appeard in V4.7.2.

@ghost
Copy link

ghost commented Jun 24, 2018

@jkotas
I tried a .net core 2.1 project and the SortedList doesn't contain these methods also!
I don't know ahat is wrong! I use VS Community 15.7.4.

@jkotas
Copy link
Member

jkotas commented Jun 24, 2018

I do not know either. Looks like you may be running into a bug in Visual Studio. You can report it using https://docs.microsoft.com/en-us/visualstudio/ide/talk-to-us

@ghost
Copy link

ghost commented Jun 24, 2018

@jkotas
This doc doesn't contain these methods:
https://docs.microsoft.com/ar-sa/dotnet/api/system.collections.generic.sortedlist-2?view=netcore-2.1

Maybe these methods exsist in a NuGet.

@ghost
Copy link

ghost commented Jun 24, 2018

@jkotas :
I got it :)
I am talking about the Generic SortedList, while you are talking about the non-generic one :D
My question stays: why the generic sortedList can't access elements by index?
Please re-open the issue.
Thanks.

@jkotas
Copy link
Member

jkotas commented Jun 24, 2018

Ah ok. Sorry about the confusion. On generic SortedList, this is available via indexers on Keys and Values properties. Try:

SortedList<int, int> sl = new SortedList<int, int>();
for (int i = 0; i < 10; i++) sl.Add(i, i*i);
Console.WriteLine(sl.Keys[5]);
Console.WriteLine(sl.Values[5]);

@ghost
Copy link

ghost commented Jun 24, 2018

@jkotas
Thanks.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Collections question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

3 participants