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 
        }
                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.
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.
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.
Multicast Delegates must have a return type of void Otherwise it will throw an exception.
public void Ip(string ip)
{
  if (Callback != null)
  {
    List<String> valueReturnedByCallback = Callback();
  }
}
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With