【问题标题】:Return value from Action动作的返回值
【发布时间】:2022-12-18 10:47:51
【问题描述】:

大家好,我需要从我的行动中获得回报,请参阅此示例..

public class B
{
    public void test()
    {

        Action asd = test2;

    }
    private void test2()
    {
        Console.WriteLine("LOL");
    }
}

这个示例工作,但我需要像这样从 test2 方法返回字节..

public class B
{
    public void test()
    {

        Action asd = test2;

    }
    private byte test2()
    {
        Console.WriteLine("LOL");
        return 0;
    }
}

任何解决方案?

【问题讨论】:

  • 这回答了你的问题了吗? How to return value from Action()?
  • 然后使用 Func<byte> 而不是 Action。 Action 用于不返回任何内容的方法,而 Func 用于返回值的方法。
  • 你真的需要 return 还是 void?

标签: c# action


【解决方案1】:

谢谢它现在可以工作了,我已经在其中编辑了我的代码..

public class B
{
    public void test()
    {

        Func<byte> asd = test2;
        byte x = asd();
        Console.WriteLine(x);
    }
    private byte test2()
    {
        Console.WriteLine("LOL");
        return 0;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 2020-02-14
    • 2011-09-07
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多