dotnet / dotnet/sdk

CA1859 shouldn't recommend using an anonymous type as return type

Open
#50,366 0 comments 0 reactions 0 assignees View on GitHub
Area-Microsoft.CodeAnalysis.NetAnalyzers untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### Analyzer

**Diagnostic ID**: [CA1859](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1859): `Use concrete types when possible for improved performance`

### Analyzer source

**SDK**: 9.0.101

### Describe the bug

When a method's return type is `object` and the returned value is an anonymous type (`new { Name = "Alex" }`) the analyzer suggests changing the method's return type from `object` to ``. Of course, this return type is invalid.

### Steps To Reproduce

```csharp
Console.WriteLine(BuildObject());

private static object BuildObject() // Triggers CA1859
{
var outputData = new
{
Name = "Alex"
};

return outputData;
}
```

### Expected behavior

Shouldn't trigger the CA1859 rule.

### Actual behavior

Triggers the CA1859 rule.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.