相关疑难解决方法(0)

在C#中将委托转换为Func

我有代码:

public delegate int SomeDelegate(int p);

public static int Inc(int p) {
    return p + 1;
}
Run Code Online (Sandbox Code Playgroud)

我可以投IncSomeDelegateFunc<int, int>:

SomeDelegate a = Inc;
Func<int, int> b = Inc;
Run Code Online (Sandbox Code Playgroud)

但我不能投IncSomeDelegate和投后Func<int, int>用这样的常用方法:

Func<int, int> c = (Func<int, int>)a; // ?ompilation error
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?

.net c# delegates casting

43
推荐指数
6
解决办法
2万
查看次数

标签 统计

.net ×1

c# ×1

casting ×1

delegates ×1