Alex Kalfoglou
Alex Kalfoglou

Reputation: 13

Explanation for lambda expression C#

public static void GetUserAccessToken(string email, string password,string deviceToken,
        Action onCompletion, Action<RestError> onError)
    {
        WebRequestBuilder builder = new WebRequestBuilder()
            .Url(GetApiUrl(Urls.USER_ACCESS_TOKEN))
            .Verb(Verbs.POST)
            .ContentType(ContentTypes.FORM)
            .FormData(Attributes.CLIENT_ID, Config.Api.PasswordGrantClientId)
            .FormData(Attributes.CLIENT_SECRET, Config.Api.PasswordGrantClientSecret)
            .FormData(Attributes.EMAIL_ID, email)
            .FormData(Attributes.PASSWORD, password)
            .FormData(Attributes.DEVICE_TOKEN, deviceToken)
            .FormData(Attributes.SCOPE, "*");

        AddClientAuthHeader(ref builder);

        _instance._restUtil.Send(builder, handler =>
        {
            var response = DataConverter.DeserializeObject<ApiResponseFormat<UserToken>>(handler.text);
            UserAccessToken = response.Data.AccessToken;
            UserRefreshToken = response.Data.RefreshToken;
            onCompletion?.Invoke();
        }, restError =>

I cant understnad this line: _instance._restUtil.Send(builder, handler => { var response = DataConverter.DeserializeObject<ApiResponseFormat<UserToken>>(handler.text); and specifically from where and how its going to get the value for handler.text since i cannot see where it is defined or passed as a parameter

Upvotes: 0

Views: 72

Answers (1)

Rafaelplayerxd YT
Rafaelplayerxd YT

Reputation: 407

Think about lambda expressions as little methods, the lambda that you referenced would be something like this:

void Foo (Handler handler) // i am assuming that handler is of type Handler here
{
    // do whatever you want
}

there the 'handler' variable is passed to the lambda as parameter just like in the method above, the text field inside handler, is just a normal instance field provided by the supposed Handler type.

Than the implementation of the Send method is responsible to pass a Handler as argument to the lambda invocation.

Upvotes: 1

Related Questions