Vocaloidas
Vocaloidas

Reputation: 370

Generic method: There's no boxing conversion from int to IComparable<T>

I'm working on a binary search algorithm, which has the following parameters:

Now when I pass these arguments:

It says that type int cannot be used as a parameter F (I was under the impression that the generic types are not concerned with types that are being passed) and that there are no 'boxing' conversion from int to IComparable.

What I was trying to do: Basically, I wanted this method to accept search key's which can be of various numeric types (ints, doubles etc.) and so in the generic method I tried to declare two types.

About this code: The func delegate represents an object's property i.e. car.Name (string), car.Wheels(int) which are of different types. I Sort of want the key data type somehow be inferred based on the propertyFields type that's being passed, but that seems way too complicated, so I tried making it so that the F Key accepts various types and just make sure that I'm passing the correct types to it.

I don't know if this all sounds confusing, but if you have questions about any of my code, feel free to ask.

Edit: The error occurs when I call the BinarySearch Method.

Edit 2: for the propertyField i pass this as an argument: c => c.Longitude (or any other object property).

Upvotes: 0

Views: 1153

Answers (1)

Scott Chamberlain
Scott Chamberlain

Reputation: 127563

Either your 2nd constraint needs to be where F : IComparable<F> or you should not have a F at all and you should be taking in a T Key instead of a F Key

Upvotes: 1

Related Questions