dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.98k stars 4.66k forks source link

Array.IndexOf option to use IComparable #40410

Open vsfeedback opened 4 years ago

vsfeedback commented 4 years ago

This issue has been moved from a ticket on Developer Community.


In an application I did not want to use bool Equals(Object^ b)override since it messed up Object.Reference equals in other parts. If I read the documentation right, Array uses equals to find a match, and with IComparable a zero would be a match. Would it be desirable to add such?


Original Comments

Feedback Bot on 4/1/2020, 08:20 PM:

Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further. We'll provide an update once the issue has been triaged by the product team.

Dotnet-GitSync-Bot commented 4 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ygra commented 4 years ago

The same might probably apply to List<T>. And perhaps IEqualityComparer<T> would be a better choice than an IComparable as only equality is needed here, not an ordering.

EDIT: Nevermind, mistook IComparable for IComparer.

GrabYourPitchforks commented 4 years ago

I don't think we have existing IndexOf methods that take an equality comparer. What's the proposal here? Is it the below?

public class Array
{
    public static int IndexOf<T>(T[] array, T value, IEqualityComparer<T> equalityComparer);
    public static int IndexOf<T>(T[] array, T value, int offset, int count, IEqualityComparer<T> equalityComparer);
}

Would it make more sense to be a LINQ extension method?

public static class Enumerable
{
    public static int IndexOf<T>(this IEnumerable<T> enumerable, T value, IEqualityComparer<T> equalityComparer);
}
svick commented 4 years ago

Somewhat related discussion about IndexOf on IEnumerable<T>: https://github.com/dotnet/runtime/issues/31156.