Cannot convert lambda expression to delegate type 'System.Func<T,TKey>'

hIpPy

I'm writing a generic class for min heap where I want to be able to heapify on TKey as well as T.

interface IHeap<T, TKey>
    where TKey : IComparable<TKey>
{
    void Insert(T x);
    T Delete();
    T Top();
}

public class MinHeap<T, TKey> : IHeap<T, TKey>
    where TKey : IComparable<TKey>
{
    public MinHeap(int capacity)
        : this(capacity, x => x) // <---- compilation error here
    { }
    public MinHeap(int capacity, Func<T, TKey> keySelector)
        : this(capacity, keySelector, Comparer<TKey>.Default)
    { }
    public MinHeap(int capacity, Func<T, TKey> keySelector, IComparer<TKey> comparer)
    {
        // ...
    }
    // ...
}

I get these compilation errors for x => x:

Cannot convert lambda expression to delegate type 'System.Func<T,TKey>' because some of the return types in the block are not implicitly convertible to the delegate return type.
Cannot implicitly convert type 'T' to 'TKey'

How do I achieve this and just have one class?

Update:

I want to be able to do two things:

// 1
var minheap = new MinHeap<Person, int>(10, x => x.Age);

// 2
var minheap = new MinHeap<int>(10);
// instead of 
var minheap = new MinHeap<int, int>(10, x => x);
Ben Aaronson

MinHeap<T,TKey> can be instantiated with any generic type parameters matching the constraints.

That means for example, you could have a MinHeap<string,int>. In that case, you'd be trying to assign lambda x => x to a Func<string,int> which wouldn't work, since it's a Func<string,string>.

I don't think there is a sensible way to achieve what you want, as there's no good candidate for a "default" way to convert one arbitrary type to another arbitrary type, which is what you'd need.


What you could do is remove this constructor and add a static constructor which could be used for the cases when the T and TKey are the same type:

public static class MinHeap
{
    public static MinHeap<T,T> Create<T>(int capacity) where T : IComparable<T>
    {
        return new MinHeap<T,T>(capacity, x => x);
    }
}

But if this isn't enough for your needs, then just remove the constructor and accept that people will have to deal with passing a lambda in themselves.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot convert lambda expression to delegate type 'System.Func<T,TKey>'

From Dev

Cannot convert lambda expression to delegate type 'System.Func<char,bool>'

From Dev

Cannot convert lambda expression to type 'System.Linq.Expressions.Expression' because it is not a delegate type

From Dev

Cannot convert lambda expression to type 'Delegate' because it is not a delegate type

From Dev

Cannot Convert Lambda expression to type 'System.Type' because it is not a Delegate Type

From Java

Cannot convert lambda expression to type 'string' because it is not a delegate type

From Dev

Cannot convert lambda expression to type "..." because it is not a delegate type

From Dev

Cannot convert lambda expression to type 'bool' because it is not a delegate type

From Dev

Error: Cannot convert lambda expression to type 'string' because it is not a delegate type

From Dev

Cannot convert lambda expression to type 'string' because it is not a delegate type MVC

From Dev

Cannot convert lambda expression to type 'object' because it is not a delegate type

From Dev

Cannot convert lambda expression to type 'string' because it is not a delegate type With NotifyOfPropertyChange

From Dev

Cannot convert lambda expression to type 'string' because it is not a delegate type MVC

From Dev

Cannot convert lambda expression to type 'Indices' because it is not a delegate type in elasticsearch

From Dev

Cannot convert lambda expression to type int because it is not a delegate type

From Dev

CheckBoxFor - Cannot convert lambda expression to intended delegate type

From Dev

"Cannot convert lambda expression to type 'string' because it is not a delegate type" querying dataset in C#

From Dev

Cannot convert lambda expression to type 'string' because it is not a delegate type - OrderBy and DbGeography by ref

From Dev

Error: Cannot convert lambda expression to type 'bool' because it is not a delegate type in Kendo Chart

From Dev

Error: Cannot convert lambda expression to type 'bool' because it is not a delegate type in Kendo Chart

From Dev

Cannot implicity convert type System.Linq.Expression<System.Func<Object, bool>> to bool

From Dev

Cannot convert from 'lambda expression' to 'system.func dynamic object ' MVC3 Razor

From Dev

mvvmcross iOS Bind to list Cannot convert `lambda expression' to non-delegate type `string'

From Dev

mvvmcross iOS Bind to list Cannot convert `lambda expression' to non-delegate type `string'

From Dev

C# show data of ExpandoObject - Cannot convert lambda expression to type '…' because it is not a delegate

From Dev

c# - Cannot convert lambda expression to delegate type; but this works in List functions

From Dev

Expression<Func< - Operator '||' cannot be applied to operands of type 'lambda expression' and 'lambda expression'

From Dev

Cannot convert from `Expression<Func<T1, T2>>` to `Expression<Func<object, object>>`

From Java

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

Related Related

  1. 1

    Cannot convert lambda expression to delegate type 'System.Func<T,TKey>'

  2. 2

    Cannot convert lambda expression to delegate type 'System.Func<char,bool>'

  3. 3

    Cannot convert lambda expression to type 'System.Linq.Expressions.Expression' because it is not a delegate type

  4. 4

    Cannot convert lambda expression to type 'Delegate' because it is not a delegate type

  5. 5

    Cannot Convert Lambda expression to type 'System.Type' because it is not a Delegate Type

  6. 6

    Cannot convert lambda expression to type 'string' because it is not a delegate type

  7. 7

    Cannot convert lambda expression to type "..." because it is not a delegate type

  8. 8

    Cannot convert lambda expression to type 'bool' because it is not a delegate type

  9. 9

    Error: Cannot convert lambda expression to type 'string' because it is not a delegate type

  10. 10

    Cannot convert lambda expression to type 'string' because it is not a delegate type MVC

  11. 11

    Cannot convert lambda expression to type 'object' because it is not a delegate type

  12. 12

    Cannot convert lambda expression to type 'string' because it is not a delegate type With NotifyOfPropertyChange

  13. 13

    Cannot convert lambda expression to type 'string' because it is not a delegate type MVC

  14. 14

    Cannot convert lambda expression to type 'Indices' because it is not a delegate type in elasticsearch

  15. 15

    Cannot convert lambda expression to type int because it is not a delegate type

  16. 16

    CheckBoxFor - Cannot convert lambda expression to intended delegate type

  17. 17

    "Cannot convert lambda expression to type 'string' because it is not a delegate type" querying dataset in C#

  18. 18

    Cannot convert lambda expression to type 'string' because it is not a delegate type - OrderBy and DbGeography by ref

  19. 19

    Error: Cannot convert lambda expression to type 'bool' because it is not a delegate type in Kendo Chart

  20. 20

    Error: Cannot convert lambda expression to type 'bool' because it is not a delegate type in Kendo Chart

  21. 21

    Cannot implicity convert type System.Linq.Expression<System.Func<Object, bool>> to bool

  22. 22

    Cannot convert from 'lambda expression' to 'system.func dynamic object ' MVC3 Razor

  23. 23

    mvvmcross iOS Bind to list Cannot convert `lambda expression' to non-delegate type `string'

  24. 24

    mvvmcross iOS Bind to list Cannot convert `lambda expression' to non-delegate type `string'

  25. 25

    C# show data of ExpandoObject - Cannot convert lambda expression to type '…' because it is not a delegate

  26. 26

    c# - Cannot convert lambda expression to delegate type; but this works in List functions

  27. 27

    Expression<Func< - Operator '||' cannot be applied to operands of type 'lambda expression' and 'lambda expression'

  28. 28

    Cannot convert from `Expression<Func<T1, T2>>` to `Expression<Func<object, object>>`

  29. 29

    Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

HotTag

Archive