Skip to content

Latest commit

 

History

History
36 lines (26 loc) · 1.06 KB

Generics.md

File metadata and controls

36 lines (26 loc) · 1.06 KB

Generics make it so a method or class can be used by several different types.

 public class ApiGatewayClient<T>
    {
        public async Task<T> PostAsync(string urlPath, T payload)
        {
            var fullUri = new Uri(apiBaseUri, urlPath);

            HttpRequestMessage httpRequestMessage = PrepareHttpRequestMessage(payload, fullUri, HttpMethod.Post);

            var response = await retryPolicy.ExecuteAsync(async () => await client.SendAsync(httpRequestMessage)
            .ConfigureAwait(false))
                .ConfigureAwait(false);

            if (!response.IsSuccessStatusCode)
            {
                throw new HttpResponseException(response.StatusCode);
            }

            var content = await response.Content.ReadAsStringAsync();

            var responseObject = JsonConvert.DeserializeObject<T>(content);

            return responseObject;

        }
    }

Also used for lists

List<int> intList = new List<int>();

Generic Classes