0

Hi guys i need return value from my action, see this sample..

public class B
{
    public void test()
    {

        Action asd = test2;

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

This sample work, but i need to return byte from test2 method like this..

public class B
{
    public void test()
    {

        Action asd = test2;

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

Any solution?

Salvosnake
  • 83
  • 10
  • 1
    Does this answer your question? [How to return value from Action()?](https://stackoverflow.com/questions/8099631/how-to-return-value-from-action) – gunr2171 Dec 07 '22 at 13:01
  • 5
    then use a `Func` instead of an `Action`. Action is for methods returning nothing, while Func is for methods returning a value. – MakePeaceGreatAgain Dec 07 '22 at 13:01
  • do you really need return or void? – Vivek Nuna Dec 07 '22 at 13:04
  • `Action` is a delegate type for a method that takes parameter `T` and returns `void`. If you need a different signature, you need a different delegate type - rather than simply replacing `Action` with `Func` and calling it a day, I'd recommend reading up on, and toying with `delegate` types and understanding how they work, for they are a fundamental component of .net and C#. – Mathieu Guindon Dec 07 '22 at 13:49

1 Answers1

0

thank you it work now, i have edited my code in this..

public class B
{
    public void test()
    {

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