© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
4 replies
DMax

✅ How to return List from RestClient?

Hi, I am making rest client for my application using RestSharp (like their twitter client in guide https://restsharp.dev/usage.html#api-client).
Everywhere in code I have something like
public record Entity1(string SomeCoolString);

public record GetSomethingResponse 
{
  [JsonPropertyName("inconsistent_name_within_whole_api")]
  public int Id { get; set; }

  [JsonPropertyName("Cool_Entities")]
  public List<Entity1> CoolEntities { get; set; }
}

public async Task<GetSomethingResponse> GetSomething()
{
  var request = new RestRequest("endpoint");
  var response = await _client.ExecuteAsync<GetSomethingResponse>(request);
  return response.Data;
}
public record Entity1(string SomeCoolString);

public record GetSomethingResponse 
{
  [JsonPropertyName("inconsistent_name_within_whole_api")]
  public int Id { get; set; }

  [JsonPropertyName("Cool_Entities")]
  public List<Entity1> CoolEntities { get; set; }
}

public async Task<GetSomethingResponse> GetSomething()
{
  var request = new RestRequest("endpoint");
  var response = await _client.ExecuteAsync<GetSomethingResponse>(request);
  return response.Data;
}
But one endpoint returns just an array of entities and I don't like how
Task<List<CoolerEntity2>>
Task<List<CoolerEntity2>>
looks
In TypeScript I could make alias
type GetEntities = CoolerEntity2[]
type GetEntities = CoolerEntity2[]
and use it. Is there something similar in C#?
Also, should I use
ICollection
ICollection
instead of
List
List
?

P.S. Models (or DTO?) and client code are in separated files
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Return List<Entity> from method with return type of List<T> where T : Entity
C#CC# / help
3y ago
✅ How to return variables from a function
C#CC# / help
3y ago