无法将lambda表达式转换为委托类型'System.Func '(Cannot convert lambda expression to delegate type '

系统教程 行业动态 更新时间:2024-06-14 17:04:02
无法将lambda表达式转换为委托类型'System.Func '(Cannot convert lambda expression to delegate type 'System.Func')

我正在为min heap编写一个泛型类,我希望能够在TKey和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) { // ... } // ... }

我得到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'

我如何实现这一目标并且只有一个班级?

更新:

我希望能够做两件事:

// 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);

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);

最满意答案

MinHeap<T,TKey>可以使用与约束匹配的任何泛型类型参数进行实例化。

这意味着,例如,您可以拥有MinHeap<string,int> 。 在这种情况下,您将尝试将lambda x => x分配给Func<string,int> ,这不起作用,因为它是一个Func<string,string> 。

我不认为有一种明智的方法来实现你想要的东西,因为没有一个好的候选者可以将一种任意类型转换为另一种任意类型的“默认”方式,这就是你所需要的。


您可以做的是删除此构造函数并添加一个静态构造函数,该构造函数可用于T和TKey类型相同的情况:

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); } }

但如果这还不足以满足您的需求,那么只需删除构造函数并接受人们将不得不处理传递lambda的问题。

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.

更多推荐

本文发布于:2023-04-24 20:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/355c7a63b438403439b2e1835e50d247.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   转换为   类型   System   lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!