0

I have this function with callback parameter Action<string> callback:

 public void sync(Action<string> callback)
        {

            var client = new RestClient(RestfulPaths.BASE_URL);
            var request = new RestRequest(RestfulPaths.SYNC_CHECK, Method.GET);
            var json = client.Execute(request);

            client.ExecuteAsync(request, response =>
            {
                callback(response.Content);
            });

        }

I tried to call this function in another part of application:

ServerSync = manager.sync({
   // Get data here
});
POV
  • 11,293
  • 34
  • 107
  • 201

1 Answers1

2

Action indicates that it is a delegate that has no return value. The type parameter string indicates that the action will take a single parameter of type string. So to use it, you have to pass a delegate that accepts a string parameter. A simple way to do this is:

manager.sync((responseContent)=>{
    Console.WriteLine(responseContent);
});
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • `Action indicates that it is a delegate that has no return value.`. What does it mean? Now it returns value is not? – POV Jul 19 '17 at 22:48
  • 2
    It doesn't return a value. In the example you provided, the delegate method is called with the `response.Content` that is retrieved from the rest call – John Koerner Jul 19 '17 at 22:51
  • So, inside delegate I can get response result? – POV Jul 19 '17 at 22:51
  • 2
    Yes, I updated the code to indicate that the variable passed to the delegate is the value that contains the content. – John Koerner Jul 19 '17 at 22:53
  • So, this delegate does not have name, is as anonymous? – POV Jul 19 '17 at 22:55
  • 2
    Yes, this would be considered anonymous. You could also define a `void` function that accepts a string and pass that in as the delegate. – John Koerner Jul 19 '17 at 22:57
  • Can you explain me case when I need to bind progressbar to async request, so I need to return the second parameter in callback? – POV Jul 19 '17 at 23:15