如何实现 Action<.> 作为 Func<..>.

本文关键字:作为 Func Action 何实现 实现 | 更新日期: 2023-09-27 17:56:46

我想将操作实现为 func 并得到错误:在这种情况下无法使用 void。请告知

Action<string> someFunc_1 = Console.WriteLine;
someFunc_1("Test");
Func<string, void> someFunc_2 = Console.WriteLine;

如何实现 Action<.> 作为 Func<..>.

Action<T1, T2, ...>是为了替换Func<T1, T2, ..., void>

不能在泛型中使用void。它不是 C# 中的类型。

那么在您的情况下,请使用 Action<string> 而不是 Func<string, void> .

Func<string, bool> someFunc_2 = s =>
{
  Console.WriteLine(s);
  return true;
};