Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read return value from delegate

Tags:

c#

delegates

I am not sure if I understood the usage of delegates correctly but I would like to read delegate return value in publisher class. The example is below with description.

//Publisher class
    public class ValidateAbuse
    {

    public delegate List<String> GetAbuseList();
    public static GetAbuseList Callback;

    public void Ip(string ip)
    {
    //   I would like to read GetAbuseList value (List<String>) here. How to do that?
    }

    }


//Subscriber class
    class Server
    {

        public static void Start()
        {
            ValidateAbuse.Callback = GetIpAbuseList;
            ValidateAbuse.Ip(MyIp);
        }

        private static List<string> GetIpAbuseList()
        {
            //return List<String> to ValidateAbuse class and use return value in public void Ip(string ip) method 
        }
like image 237
Tomas Avatar asked Apr 16 '12 12:04

Tomas


People also ask

Can a delegate return a value?

If you have a method that accepts two numbers and you want to add them and return the sum of the two numbers, you can use a delegate to store the return value of the method as shown in the code snippet given below.

What is a delegate C$?

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.

What is TResult in C#?

Func<TResult> represents a method taking 0 arguments and returning an object of TResult , whereas Action<T> represents a method returning void. You need two different delegates as you can't specify void as a type argument.

What should be the return type of multicast delegate methods?

Multicast Delegates must have a return type of void Otherwise it will throw an exception.


2 Answers

public void Ip(string ip)
{
  if (Callback != null)
  {
    List<String> valueReturnedByCallback = Callback();
  }
}
like image 54
Hari Avatar answered Oct 17 '22 21:10

Hari


Here's a version that does not use static for ValidateAbuse and that uses the built-in Func<T> delegate.

public class ValidateAbuse
{
    private Func<List<string>> callback;

    public ValidateAbuse(Func<List<string>> callback)
    {
        this.callback = callback;
    }

    public void Ip(string ip)
    {
        var result = callback();
    }
}

public class Server
{
    public static void Start()
    {
        var validateAbuse = new ValidateAbuse(GetIpAbuseList);
        validateAbuse.Ip(MyIp);
    }

    private static List<string> GetIpAbuseList()
    {
        //return List<string> to ValidateAbuse class and use return value in public void Ip(string ip) method 
    }
}

I recommend you avoid static since that gives you a global state, which could later give you coupling problems and also makes it hard for you to unit test.

The other answers given so far has a guard clause, checking Callback for null. Unless that is expected behaviour (that Callback is null) I would avoid this. It's better to crash early than to get hard to debug errors later on.

I would also try to make the Server non-static.

like image 25
Torbjörn Kalin Avatar answered Oct 17 '22 21:10

Torbjörn Kalin