Skip to content

There is a performance issue with WeatherForecastController.cs  #45353

@yangzhongke

Description

@yangzhongke

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

In the templates of ASP.NET Core Web API, the Get() method of 'aspnetcore/src/ProjectTemplates/Web.ProjectTemplates/content/WebApi-CSharp/Controllers/WeatherForecastController.cs ' is as follows:

public IEnumerable<WeatherForecast> Get()
{
	return Enumerable.Range(1, 5).Select(index => new WeatherForecast
	{
		Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
		TemperatureC = Random.Shared.Next(-20, 55),
		Summary = Summaries[Random.Shared.Next(Summaries.Length)]
	})
	.ToArray();
}

The return value of Get() method is of IEnumerable, and the Select() method returns the same type, so there was no need to convert it to an array using ToArray().
Furthermore, it can cause huge memory consumption problem, and I have stated it in a post: https://www.reddit.com/r/aspnetcore/comments/z7jk8r/who_says_net_doesnt_have_gc_tuning_changing_one/

Describe the solution you'd like

I suggest that remove the invocation of 'ToArray()', which could set up a better model for .NET developers.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions