how do you refactor c code that returns a listt?

The variable parts are the return type and the URI, so if you declare your method like this:

private async Task<List<T>> Get<T>(string requestUri)
        var client = ClientFactory.CreateClient("ServerAPI");
        return await client.GetFromJsonAsync<List<T>>(requestUri);
    catch (AccessTokenNotAvailableException exception)
    return new List<T>();

Then you can call like this:

masterData.PatternData = await Get<MeetingPatternData>("MeetingTimeManager/GetPatterns");
masterData.PatternDataDays = await Get<MeetingPatternDays>("MeetingTimeManager/GetDays");

I’ve removed this part:

var retVal = new List<MeetingPatternData>();

Because your allocating a List unnecessarily.

You should probably also be returning an IEnumerable<T> rather than List<T> based on the philosophy of using the most restrictive type necessary to do the job, unless the caller needs to manipulate the returned data.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top